Android 安卓将适配器添加到ListView时出现NullPointerException?

Android 安卓将适配器添加到ListView时出现NullPointerException?,android,android-listview,android-adapter,Android,Android Listview,Android Adapter,我找不到问题。在这种情况下,我检查了很多问题,但我无法解决问题。我粘贴了来自另一个源代码的代码,效果很好。这是代码和XML文件,也许有人能告诉你问题出在哪里。有什么建议吗 public class MainActivity extends Activity { MySQLiteHelper db = new MySQLiteHelper(this); public String address = new String(); private com.example.fdfd.DiscussAr

我找不到问题。在这种情况下,我检查了很多问题,但我无法解决问题。我粘贴了来自另一个源代码的代码,效果很好。这是代码和XML文件,也许有人能告诉你问题出在哪里。有什么建议吗

public class MainActivity extends Activity {
MySQLiteHelper db = new MySQLiteHelper(this);
public String address = new String();

private com.example.fdfd.DiscussArrayAdapter adapter;
public List firstMessages;




public String messageBody = new String();
public String messageDate = new String();
public String messageBoolean = new String();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_discuss);

    final ListView listview = (ListView) findViewById(R.id.listView);
    listview.setClickable(true);

    final ArrayAdapter newAdapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, firstMessages);

    listview.setAdapter(newAdapter);
.
.
.
我的XML:

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

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

您的列表,
firstMessages
,为空,这将生成NullPointerException。当附加到ListView时,会调用
getCount()
,然后调用集合上的
size()

您从未初始化firstMessages,因此它为空。 尝试更改以下部分

public List firstMessages;


ArrayAdapter的作用是将ListView的每个项与布局和数据相关联


ArrayAdapter的数据有问题,它们从未初始化过。然后,您不能将listview中的项目与数据关联,也不能在版面上显示这些项目。

public List firstMessages;是null,因为您得到了null指针exeption
public List firstMessages;
public List firstMessages = new ArrayList();