2014-01-17 74 views
0

1.如何設置onItemClickListener,以便如果我進入我的SMS收件箱並單擊某個聯繫人,它將打開顯示我與該聯繫人之間的對話視圖的新活動?如何在我的短信應用程序中設置onItemClickListener

2.我的第二個問題是我的代碼應該放在哪裏onItemClickListener?

package techyapp.greg.spanglish; 

import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class Inbox extends Activity implements OnItemClickListener { 

    ListView lViewSMS; 
    String thread_id, body; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.inbox); 

     ListView list = (ListView) findViewById(R.id.lvlistViewSMS); 
     List<String> msgList = getSMS(); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, msgList); 
     list.setAdapter(adapter); 

    } 

    public List<String> getSMS() { 
     List<String> sms = new ArrayList<String>(); 
     Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/"); 
     Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, 
       "date"); 

     while (cur.moveToNext()) { 
      String address = cur.getString(cur.getColumnIndex("address")); 
      String body = cur.getString(cur.getColumnIndexOrThrow("body")); 
      String person = cur.getString(cur.getColumnIndexOrThrow("person")); 
      String thread_id = cur.getString(cur 
        .getColumnIndexOrThrow("thread_id")); 
      sms.add(address + " " + person + "\n" + body); 

     } 
     return sms; 

    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 
     Intent myIntent = new Intent(getApplicationContext(), 
       Conversation.class); 
     startActivity(myIntent); 
    } 
}  

01-21 15:33:33.674:E/AndroidRuntime(4749):致命異常:主

01-21 15:33:33.674:E/AndroidRuntime(4749): java.lang.RuntimeException:無法實例化活動ComponentInfo {techyapp.greg.spanglish/techyapp.greg.spanglish.Conversation}:java.lang.NullPointerException

01-21 15:33:33.674:E/AndroidRuntime(4749 ):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)

01-21 15:33:33.674:E/AndroidRuntime(4749):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)

01-21 15:33:33.674:E/AndroidRuntime (4749):at android.app.ActivityThread.access $ 600(ActivityThread.java:153)

01-21 15:33:33.674:E/AndroidRuntime(4749):at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1269)

01-21 15:33:33.674:E/AndroidRuntime(4749):在android.os.Handler.dispatchMessage(Handler.java:99)

01-21 15:33:33.674:E/AndroidRuntime(4749):at android.os.Looper.loop(Looper.java:137)

01-21 15:33:33.674:E/AndroidRuntime(4749) ):at android.app.ActivityThread.main(ActivityThread.java:5289)

01-21 15:33:33.674:E/AndroidRuntime(4749):at java.lang.reflect.Method.invokeNative(Native Method )

01-21 15:33:33.674:E/AndroidRuntime(4749):在java.lang.reflect.Method.invoke(Method.java:525)

01-21 15:33:33.674 :E/AndroidRuntime(4749):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(Zy goteInit.java:739)

01-21 15:33:33.674:E/AndroidRuntime(4749):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)

01 -21 15:33:33.674:E/AndroidRuntime(4749):at dalvik.system.NativeStart.main(Native Method)

01-21 15:33:33.674:E/AndroidRuntime(4749):引起:顯示java.lang.NullPointerException

01-21 15:33:33.674:E/AndroidRuntime(4749):在techyapp.greg.spanglish.Conversation(Conversation.java:23)

01-21 15:33:33.674:E/AndroidRuntime(4749):at java.lang.Class.newInstanceImpl(Native Method)

01-21 15:33:33.674:E/AndroidRuntime(4749):at java.lang.Class.newInstance(Class.java:1130)

01-21 15:33:33。674:E/AndroidRuntime(4749):at android.app.Instrumentation.newActivity(Instrumentation.java:1061)

01-21 15:33:33.674:E/AndroidRuntime(4749):at android.app.ActivityThread .performLaunchActivity(ActivityThread.java:2246)

01-21 15:33:33.674:E/AndroidRuntime(4749):... 11多個

回答

0

看起來你忘了該項目的點擊監聽器添加到列表顯示。

list.setOnItemClickListener(this); 

您還需要將相關數據傳遞給新活動。

你可以做這樣的事情

Intent myIntent = new Intent(getApplicationContext(), 
      Conversation.class); 
    myIntent.putExtra("msg", myMsgData); 
    startActivity(myIntent); 

記住myMsgData必須是少數支持的格式之一,以活動之間傳遞。看看這個職位How do I pass data between Activities in Android application?

+0

我已經看過帖子,並嘗試瞭解決方案的一些解決方案是一個很大的幫助,我相信我輸入正確的數據。我遇到的錯誤是你的課程。當我點擊我的聯繫人時,它崩潰。 – user3200079

+0

如果發生崩潰,您需要發佈崩潰的logcat輸出。 – sgarman

相關問題