0

在构建用户将在我的 android 应用程序上相互发送消息的页面上,我正在使用列表视图来显示所有发送或接收的消息。对于初始测试,我尝试向自己发送一条消息,但它使应用程序崩溃并且错误消息让我感到困惑。它说

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>

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

4

3 回答 3

1

messageList一片空白。您应该实例化和填充messageList,而不是现在的方式。

即使这样,您也没有SimpleAdapter以正确的方式使用。立即查看此处此处以使用它。

于 2013-07-17T06:50:27.697 回答
1

愿这能帮助你..

SimpleAdapter在参数 2 中采用 List 的构造函数,例如:SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] 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);
于 2013-07-17T06:55:17.463 回答
0

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

于 2013-07-17T06:52:59.800 回答