我收到这些Listview错误导致我的应用程序崩溃

时间:2013-07-17 06:47:43

标签: 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-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;
    }

}

这是我的list_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>

和我的activity_send_message布局

<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>

所以是的,我真的不会发生什么问题。

3 个答案:

答案 0 :(得分:1)

messageList为空。您应该实例化并填充messageList,而不是现在的方式。

即使这样,您也没有以正确的方式使用SimpleAdapter。现在看看herehere即可使用它。

答案 1 :(得分:1)

这可以帮助你..

SimpleAdapter的构造函数将参数2中的List视为:SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to);

所以,这样做是为了在Adapter中添加值对:

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);

答案 2 :(得分:0)

您正在将messageList传递给适配器,但您从未创建该列表ArrayList<HashMap<String, String>> messageList;,因此当您传递它时它将为null