我使用此代码来设置视图
setContentView(R.layout.main_activity);
然后我有这段代码可以在屏幕上显示信息
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);
但我遇到的问题是我需要在 simplecursoraapter 中的每个项目行中添加一个切换按钮,但是如果我将它放入 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>
日志猫:
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