2015-11-08 99 views
0

我在我的應用程序中運行我的自定義CursorAdapter有幾個問題。根據我的logcat,錯誤,我得到:Android CursorAdapter給出java.lang.NullPointerException錯誤

11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException 
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71) 
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main 

發生在這臺適配器(obj.setAdapter(myAdapter))線71。最初我以爲我的數據庫數據檢索功能(Cursor chatCursor = mydb.selectConversation(msgId);)沒有返回任何數據,但用chatCursor.getCount()函數測試後,我意識到情況並非如此。請幫助我解決這個問題。以下是我的活動,適配器和logcat的代碼。提前致謝。

MyAdapter.java

public class MyAdapter extends CursorAdapter { 
    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    public MyAdapter(Context context, Cursor cursor) { 
     super(context, cursor, 0); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return LayoutInflater.from(context).inflate(R.layout.chat_left, parent, false); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView user = (TextView) view.findViewById(R.id.reply_user); 
     TextView msg = (TextView) view.findViewById(R.id.reply_msg); 

     String theTime = cursor.getString(cursor.getColumnIndexOrThrow("message_id")); 
     String theMessage = cursor.getString(cursor.getColumnIndexOrThrow("message")); 

     user.setText(theTime); 
     msg.setText(String.valueOf(theMessage)); 
    } 
} 

MyActivity.java

public class MyActivity extends ListActivity { 
    //Utils Class 
    Utils util; 

    final Context context = this; 
    private ProgressBar dialog; 

    ListView obj; 
    DBHelper mydb; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view); 
     util = new Utils(this); 
     mydb = new DBHelper(this); 

     Bundle extras = getIntent().getExtras(); 
     final Integer msgId = extras.getInt("id"); 

     obj = (ListView) findViewById(R.id.list); 

     Cursor chatCursor = mydb.selectConversation(msgId); 
     MyAdapter myAdapter = new MyAdapter(this, chatCursor); 
     obj.setAdapter(myAdapter); 
    } 
} 

logcat的

11-08 06:41:03.228 6109-6109/? W/System.err? java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException 
11-08 06:41:03.229 6109-6109/? W/System.err? at com.app.name.MyActivity.onCreate(MyActivity.java:71) 
11-08 06:41:03.231 6109-6109/? E/AndroidRuntime? FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MyActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) 
      at android.app.ActivityThread.access$600(ActivityThread.java:165) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
      at android.os.Handler.dispatchMessage(Handler.java:107) 
      at android.os.Looper.loop(Looper.java:194) 
      at android.app.ActivityThread.main(ActivityThread.java:5370) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.app.name.MyActivity.onCreate(MyActivity.java:71) 
      at android.app.Activity.performCreate(Activity.java:5122) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315) 
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) 
       at android.app.ActivityThread.access$600(ActivityThread.java:165) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5370) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
at dalvik.system.NativeStart.main(Native Method) 
11-08 06:41:03.238  559-832/? W/ActivityManager? Force finishing activity com.app.name/.MyActivity 
11-08 06:41:03.742  559-574/? W/ActivityManager? Activity pause timeout for ActivityRecord{42e71938 u0 com.app.name/.MyActivity} 

回答

0

看看您的行爲ivity_view.xml。當你嘗試使用findViewById(R.layout.list)查找它時,可能listview ID不是「list」。

+0

是的,它有id @android:id/list。嘗試將其更改爲'@ + id/list',但爆發了一個錯誤@Fer – Biko

+0

嘗試使用@android:+ id/list – Fer

+0

它給出了錯誤,找不到與給定名稱匹配的資源(id爲值'@android :+ id/list') – Biko

相關問題