2013-07-17 2 views
0

사용자가 내 안드로이드 응용 프로그램에서 서로 메시지를 보내고 보내거나받는 모든 메시지를 표시하는 데 listview를 사용하고 있습니다. 초기 테스트에서 나 자신에게 메시지를 보내려고했으나 응용 프로그램이 충돌하고 오류 메시지가 나를 어지럽 힙니다. 그것은내 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; 
    } 

} 

을 위해 무엇을

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

그래서 내 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 레이아웃입니다 말한다 그래, 난 정말로 무슨 일있어.

답변

1

이 당신을 도울 .. SimpleAdapter

생성자처럼 인수 2 목록 소요 월 : 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); 
+0

와우. 고마워요. –

+0

친구 환영 ... –

1

messageList은 null입니다. 지금 수행중인 방식 대신 messageList을 인스턴스화하고 채워야합니다.

그럼에도 불구하고 SimpleAdapter을 올바르게 사용하고 있지 않습니다. 사용하려면 지금 here 또는 here을보십시오.

+0

ohhhhh! 내 잘못이야! 네 말이 맞아, 나는 그것을 잊었다. –

0

messageList을 어댑터에 전달했지만 해당 목록을 생성하지 않은 경우 ArrayList<HashMap<String, String>> messageList;이므로 전달할 때 null이됩니다.

관련 문제