0

我想通过使用片段来做待办事项列表应用程序。在动态创建此片段时,我在此行(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
4

3 回答 3

0

移动这条线

listToDo = (ToDoListFragment) fm.findFragmentById(R.id.list_view_fragment);

进入你的 else 声明。

您在第一次运行时基本上是listToDo无效的,因为片段管理器不会找到您刚刚提交的片段。commit()安排要添加的片段。这并不意味着它们会立即添加。因此,findFragmentById当您立即调用它时,调用将返回 null,这就是listToDo最终为 null 的原因。

于 2013-10-30T17:24:16.560 回答
0

您的 toDoItems 未在以下行中初始化:

todoItems = savedInstanceState.getStringArrayList("todoItemTag");

我建议您测试数组以确保它不为空。就像是:

Log.i("ARRAY",todoItems.length);
于 2013-10-30T17:16:52.890 回答
0

由于todoItems是该行中三个参数中唯一一个可能为空的参数,因此它一定是罪魁祸首。我认为它为空的唯一方法是如果savedInstanceState不包括"todoItemTag"在许多情况下很容易发生的情况。您的代码假设savedInstanceState总是有"todoItemTag"ifsavedInstanceState不为空,但它很可能是非空的并且其中没有任何内容。我会像这样重写你的代码,但实际上我不明白为什么你只在某些情况下创建片段,所以这可能需要更多的工作:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState!=null)
    todoItems = savedInstanceState.getStringArrayList("todoItemTag");

if (todoItems == null) {
    todoItems = new ArrayList<String>();
}

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
listToDo.setListAdapter(adapter);

//TODO fragment management

}
于 2013-10-30T17:22:04.540 回答