我想通过使用片段来做待办事项列表应用程序。在动态创建此片段时,我在此行(in 中mainactivity.java
)采用了空指针异常:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
listToDo.setListAdapter(adapter);
我在 logcat 上看不到这条消息:Log.w("6", "altı"); 我的代码有什么问题?我怎么解决这个问题?
activity_main.xml
文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<FrameLayout
android:id="@+id/list_view_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
> </FrameLayout>
</LinearLayout>
to_do_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/add_to_do_fragment" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/addButton"
android:contentDescription="@string/addItemContentDescription"
android:text="@string/add_button"/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/addButton"
android:hint="@string/edit_text" />
</RelativeLayout>
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"></ListView>
</RelativeLayout>
LogCat
10-30 19:28:26.355: E/AndroidRuntime(1763): FATAL EXCEPTION: main
10-30 19:28:26.355: E/AndroidRuntime(1763): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.todolistwithdynamicfragments/com.example.todolistwithdynamicfragments.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080003 (com.example.todolistwithdynamicfragments:id/add_to_do_fragment) for fragment AddToDoFragment{41770868 #0 id=0x7f080003}
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.os.Looper.loop(Looper.java:137)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-30 19:28:26.355: E/AndroidRuntime(1763): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 19:28:26.355: E/AndroidRuntime(1763): at java.lang.reflect.Method.invoke(Method.java:525)
10-30 19:28:26.355: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-30 19:28:26.355: E/AndroidRuntime(1763): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 19:28:26.355: E/AndroidRuntime(1763): at dalvik.system.NativeStart.main(Native Method)
10-30 19:28:26.355: E/AndroidRuntime(1763): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080003 (com.example.todolistwithdynamicfragments:id/add_to_do_fragment) for fragment AddToDoFragment{41770868 #0 id=0x7f080003}
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.Activity.performStart(Activity.java:5142)
10-30 19:28:26.355: E/AndroidRuntime(1763): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-30 19:28:26.355: E/AndroidRuntime(1763): ... 11 more