2012-05-30 30 views
0

我用這個代碼來設置視圖由於切換按鈕的空指針?

setContentView(R.layout.main_activity); 

和我的話,我有這樣的代碼來獲得infromation了在屏幕上

Cursor MainPageCursor = databaseHelper.fetchAllReminders(); 
    startManagingCursor(MainPageCursor); 


    String[] empty = new String[] { DatabaseStore.MUTE_TITLE, DatabaseStore.START_KEY_TIME, DatabaseStore.END_KEY_TIME, DatabaseStore.COVERT_DAYS }; 



     int[] notempty = new int[]{R.id.text1,R.id.text2, R.id.text4, R.id.text5}; 
     SimpleCursorAdapter mainPageList = new SimpleCursorAdapter(this, R.layout.editinfo_row,MainPageCursor, empty, notempty); 
     setListAdapter(mainPageList); 

,但我的問題是我需要得到一個切換按鈕到simplecursoradapter中的每個項目行,但如果我把它放入simplecursoradapter佈局R.layout.editinfo_row我得到一個空指針異常我如何解決這個問題?

更新 - editinfo_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/widget38" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:orientation="vertical" > 

     <TextView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/text1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical" 
      android:textSize="25sp" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 


      <TextView 
       android:id="@+id/text2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="30sp" /> 
    <TextView 
       android:id="@+id/text3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text=" - " 
       android:textSize="30sp" /> 
      <TextView 
       android:id="@+id/text4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="30sp" /> 


     <ToggleButton 
     android:id="@+id/toggleButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
      android:textOn="main" 
     android:textOff="off" 
     android:text="ToggleButton" /> 

</LinearLayout> 



    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 
      <TextView 
       android:id="@+id/text5" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="15sp" /> 

     </LinearLayout> 

    </LinearLayout> 

的logcat:

05-30 16:31:14.249: E/AndroidRuntime(21484): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seanoneill.android.SilentPhone/com.seanoneill.android.SilentPhone.MainActivity}: java.lang.NullPointerException 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.os.Looper.loop(Looper.java:123) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.app.ActivityThread.main(ActivityThread.java:3729) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at java.lang.reflect.Method.invokeNative(Native Method) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at java.lang.reflect.Method.invoke(Method.java:507) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at dalvik.system.NativeStart.main(Native Method) 
05-30 16:31:14.249: E/AndroidRuntime(21484): Caused by: java.lang.NullPointerException 
05-30 16:31:14.249: E/AndroidRuntime(21484): at com.seanoneill.android.SilentPhone.MainActivity.addListenerOnButton(MainActivity.java:190) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at com.seanoneill.android.SilentPhone.MainActivity.onCreate(MainActivity.java:47) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-30 16:31:14.249: E/AndroidRuntime(21484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
05-30 16:31:14.249: E/AndroidRuntime(21484): ... 11 more 
+0

您應該顯示用於將按鈕放入佈局中的代碼(xml或java)以及顯示錯誤的logcat。你給出的東西並沒有告訴我們當你試圖添加按鈕時你做錯了什麼。 – Barak

+0

我加了logcat和xml – Michael

+0

好的,問題是沒有添加按鈕,它是在你的監聽器中的東西......'在com.seanoneill.android.SilentPhone.MainActivity.addListenerOnButton(MainActivity.java:190)''。這條線是什麼(可能是最有用的發佈包含該行的整個方法)。猜測你試圖在你的適配器之外設置一個監聽器......當按鈕有多個實例時,這將不起作用。您需要在自定義SimpleCursorAdapter中設置偵聽器。 – Barak

回答

0

聽起來像是你應該實現自己的適配器,覆蓋SimpleCursorAdapter.getView()得到你想要的東西。 Here是我找到的一個示例

+0

謝謝看看它 – Michael