0

此問題可能看起來像重複的問題。但這些查詢並沒有幫助我解決我的問題。錯誤/ AndroidRuntime(775):java.lang.RuntimeException:無法啓動活動ComponentInfo {..}:java.lang.NullPointerException

我想在SimpleCursorAdapter中填充(嬰兒名稱)列表中實現「onClick」事件。當用戶點擊一個名字時,就會生成一個吐司,顯示數據庫中記錄的「id」。

這裏是我的代碼 -

public class CommonNames extends Activity { 

TextView rowview; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.common_names); 

    rowview = (TextView)findViewById(R.id.commonName); 

    CommonNamesAdapter cnTable = new CommonNamesAdapter(this); 
    ListView cnListView = (ListView)findViewById(R.id.common_name_layout); 

    cnTable.open(getApplicationContext()); 
    Cursor c = cnTable.fetch_all_common_names_only(); 
    startManagingCursor(c); 

    if(c!=null){ 
     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
       R.layout.listview, 
       c, 
       new String[] {c.getColumnName(0), c.getColumnName(1)}, 

       new int[] {R.id.rowLayout,R.id.commonName}); 

       cnListView.setAdapter(adapter); 

     } 


    rowview.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      Context context = getApplicationContext(); 

      String id = context.getString(R.id.commonName); 
      CharSequence text = "This id of this item is... "+id ; 
      int duration = Toast.LENGTH_SHORT; 

      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

     } 


    }); 
    cnTable.close(); 
} 

這裏是listview.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rowLayout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 
<TextView 
    android:id="@+id/commonName" 
    android:layout_alignParentLeft="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="16dp" 
    android:paddingLeft="14dp" 
    android:paddingTop="4dp" 
    android:paddingBottom="4dp" 
/> 

</LinearLayout> 

這裏是common_names.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 
<ListView 
    android:id = "@+id/common_name_layout" 
    android:layout_width="fill_parent" 
    android:layout_height= "wrap_content" > 
</ListView> 
</LinearLayout> 

當我運行我的應用程序,我得到一個運行時NullPointerException。這是LogCat。請幫我解決這個問題。

11-13 18:23:27.852: INFO/COMMON_NAMES_TABLE(775): Inserting record... 
11-13 18:23:27.902: INFO/COMMON_NAMES_TABLE(775): OPening DataBase Connection.... 
11-13 18:23:28.002: DEBUG/AndroidRuntime(775): Shutting down VM 
11-13 18:23:28.002: WARN/dalvikvm(775): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775): FATAL EXCEPTION: main 
11-13 18:23:28.012: ERROR/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{balu.android/balu.android.CommonNames}: java.lang.NullPointerException 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.os.Looper.loop(Looper.java:123) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at dalvik.system.NativeStart.main(Native Method) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775): Caused by: java.lang.NullPointerException 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at balu.android.CommonNames.onCreate(CommonNames.java:42) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
11-13 18:23:28.012: ERROR/AndroidRuntime(775):  ... 11 more 
11-13 18:23:28.032: WARN/ActivityManager(59): Force finishing activity balu.android/.CommonNames 
11-13 18:23:28.032: WARN/ActivityManager(59): Force finishing activity balu.android/.Select 
11-13 18:23:28.585: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{45025710 balu.android/.CommonNames} 
11-13 18:23:31.192: INFO/Process(775): Sending signal. PID: 775 SIG: 9 
11-13 18:23:31.212: INFO/ActivityManager(59): Process balu.android (pid 775) has died. 
11-13 18:23:31.212: INFO/WindowManager(59): WIN DEATH: Window{45084738 balu.android/balu.android.BabyNamesAppActivity paused=false} 

謝謝。

+0

你有CommonNames空值.java line 42. NPE(通常)是最容易解決的例外情況。您應該瞭解調試器的工作原理。嘗試一下!順便說一句:沒有代碼的例外對我們來說毫無價值......並且不,沒有人想要瀏覽github來搜索特定的代碼... – WarrenFaith

+0

好的!我會在這裏保存我的代碼。謝謝。 –

回答

2

您不應該在行視圖上設置onClickListener。只有設置onclick事件一行,如果沒有內容就會崩潰(這可能是你的問題)

嘗試使用

cnListView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView parent, View v, int position, long id) 
    { 
    } 
}); 
+0

同意。它會崩潰,因爲沒有元素與'R.id.commonName'尚未(因此rowview爲空)。由於您使用的是ListView,所以在'onResume()'期間/之後的某個時刻,實際的組件會被渲染,直到ListView本身被佈置。 – dmon

+0

@Maria,我改變了你的說法。現在我得到另一個錯誤。 ' 致命例外︰主 錯誤/ AndroidRuntime(805):java.lang.IllegalStateException:android.widget.LinearLayout不是可以由此SimpleCursorAdapter約束的視圖 11-13 19:42:04.462:ERROR/AndroidRuntime' –

+0

@dmon這裏是我在onResume()實現它後的錯誤鏈接()[link](http://stackoverflow.com/questions/8112491/error-androidruntime805-java-lang-illegalstateexception-android-widget-linea )。 –

0

你common_names佈局具有的commonName所以rowview是id的視圖null和roview.setOnClickListener()將NPE。

實際的

...(775): Caused by: java.lang.NullPointerException 
...(775):  at balu.android.CommonNames.onCreate(CommonNames.java:42) 
在您粘貼它應該很容易找出這個堆棧跟蹤

錯誤消息,因爲NPE是CommonNames.java線42

相關問題