gpt4 book ai didi

java - ArrayList 已初始化,但在尝试 .add(string) 方法时仍返回 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:14:30 24 4
gpt4 key购买 nike

我在尝试使用 ArrayList 的 add 方法时遇到此错误。我可以看到很多人都遇到同样的问题,但他们在向数组列表添加元素之前没有初始化数组列表。但我正在尝试添加,并且,如果消息数组列表在该方法之前为空,我仍然要求控制台返回,并且它返回 false。

public class ShowTalkActivity extends Activity implements OnClickListener {

private ListView listView;
ArrayList<String> messages;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_talk);

Intent intent = getIntent();
String selected = intent.getStringExtra("selected");
setTitle(selected);

listView = (ListView) findViewById(R.id.messagesList);
messages = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this,R.layout.message_list_row, R.id.messagesList, messages);
listView.setAdapter(adapter);

((Button) findViewById(R.id.sendMessage)).setOnClickListener(this);
}

...

@Override
public void onClick(View view) {
System.out.println(messages==null);
messages.add("Anything");
adapter.notifyDataSetChanged();
}
}

System.out.println(messages==null);打印错误。我还尝试在开始时初始化消息 ArrayList,而不是仅仅声明。有谁知道发生了什么事吗?谢谢!

更新

这是 logcat 跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15596)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

MainActivity.java

package com.example.example;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity implements OnClickListener {

private ListView listView;
ArrayList<String> messages;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setTitle("Setted title");

listView = (ListView) findViewById(R.id.messagesList);
messages = new ArrayList<String>();
adapter = new ArrayAdapter(this, R.layout.message_list_row,
R.id.messagesList, messages);

listView.setAdapter(adapter);

Button sendmessage = (Button) findViewById(R.id.sendMessage);
sendmessage.setOnClickListener(this);

}

@Override
public void onClick(View view) {
System.out.println(messages == null);
messages.add("Anything");
adapter.notifyDataSetChanged();
}
}

acitivity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
android:id="@+id/messagesList"
android:layout_width="match_parent"
android:layout_height="322dp" >
</ListView>

<Button
android:id="@+id/sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>

message_list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/messagesList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

上面的代码产生了Result

关于java - ArrayList 已初始化,但在尝试 .add(string) 方法时仍返回 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com