Android I';我收到这些Listview错误,导致我的应用程序崩溃

Android I';我收到这些Listview错误,导致我的应用程序崩溃,android,Android,在构建一个页面时,用户将在我的android应用程序上相互发送消息,而我正在使用listview显示所有发送或接收的消息。在最初的测试中,我试图给自己发送一条消息,但它使应用程序崩溃,错误消息让我困惑。上面说 07-17 02:38:26.847: E/AndroidRuntime(2581): FATAL EXCEPTION: main 07-17 02:38:26.847: E/AndroidRuntime(2581): java.lang.NullPointerException 07-1

在构建一个页面时,用户将在我的android应用程序上相互发送消息,而我正在使用listview显示所有发送或接收的消息。在最初的测试中,我试图给自己发送一条消息,但它使应用程序崩溃,错误消息让我困惑。上面说

07-17 02:38:26.847: E/AndroidRuntime(2581): FATAL EXCEPTION: main
07-17 02:38:26.847: E/AndroidRuntime(2581): java.lang.NullPointerException
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:93)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.widget.ListView.setAdapter(ListView.java:460)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.app.ListActivity.setListAdapter(ListActivity.java:265)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at com.example.reflaptry1.SendMessage$1.onClick(SendMessage.java:39)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.view.View.performClick(View.java:4084)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.view.View$PerformClick.run(View.java:16966)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.os.Handler.handleCallback(Handler.java:615)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.os.Looper.loop(Looper.java:137)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at java.lang.reflect.Method.invokeNative(Native Method)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at java.lang.reflect.Method.invoke(Method.java:511)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-17 02:38:26.847: E/AndroidRuntime(2581):     at dalvik.system.NativeStart.main(Native Method)
下面是我对sendMessage类的介绍

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class SendMessage extends ListActivity {
    ArrayList<HashMap<String, String>> messageList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_message);

        Button sendText=(Button)findViewById(R.id.sendChat);
        sendText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EditText editMsg=(EditText)findViewById(R.id.theMessage);
                String msg=editMsg.getText().toString();
                HashMap<String, String> msgList=new HashMap<String, String>();
                msgList.put("yourMsg",msg);

                ListAdapter adapter= new SimpleAdapter(
                        SendMessage.this, messageList,
                        R.layout.list_message, new String[]{"yourMsg"},
                        new int[]{R.id.senderMessage});
                    setListAdapter(adapter);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.send_message, menu);
        return true;
    }

}
import java.util.ArrayList;
导入java.util.HashMap;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ListAdapter;
导入android.widget.simpledapter;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
公共类SendMessage扩展ListActivity{
ArrayList消息列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u send\u message);
按钮sendText=(按钮)findViewById(R.id.sendChat);
sendText.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
EditText editMsg=(EditText)findViewById(R.id.theMessage);
字符串msg=editMsg.getText().toString();
HashMap msgList=新的HashMap();
msgList.put(“yourMsg”,msg);
ListAdapter=新的SimpleAdapter(
SendMessage.this,messageList,
R.layout.list_消息,新字符串[]{“yourMsg”},
新的int[]{R.id.senderMessage});
setListAdapter(适配器);
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.send_message,menu);
返回true;
}
}
这是我的列表_message.xml

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

    <!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
    <TextView
        android:id="@+id/pid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />


    <!-- Name Label -->
    <TextView
        android:id="@+id/senderName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dip"
        android:paddingBottom="16dip"
        android:paddingLeft="16dip"

         />

    <TextView
        android:id="@+id/senderMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

</LinearLayout>

和我的活动发送消息布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".SendMessage" >

    <EditText
        android:id="@+id/theMessage"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="15dp"
        android:ems="10"
        android:hint="@string/write" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="350dp"
        android:layout_above="@+id/theMessage"
        android:layout_alignLeft="@+id/theMessage"
        android:layout_marginBottom="14dp" >
    </ListView>

    <Button
        android:id="@+id/sendChat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/theMessage"
        android:layout_toRightOf="@+id/theMessage"
        android:text="@string/button_send" />

</RelativeLayout>


所以是的,我真的不知道出了什么问题。

messageList
为空。您应该实例化并填充
messageList
,而不是现在这样做


即使如此,您也没有以正确的方式使用
simpledapter
。现在查看或打开以使用它。

您正在将
messageList
传递给适配器,但您从未创建过该列表
ArrayList messageList这可以帮助您

simpledapter
的构造函数获取参数2中的列表,如:
simpledapter(上下文上下文,列表>数据,int资源,字符串[]from,int[]to)

因此,要在适配器中添加值对,请执行以下操作:

List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

    HashMap<String, String> hm = new HashMap<String, String>();
    HashMap<String, String> hm1 = new HashMap<String, String>();
    hm.put("Name","abc");
    hm1.put("Password","abc");
    aList.add(hm);
List aList=new ArrayList();
HashMap hm=新的HashMap();
HashMap hm1=新的HashMap();
hm.put(“名称”、“abc”);
hm1.输入(“密码”、“abc”);
添加(hm);