在动态创建片段时如何摆脱“空指针异常”?

时间:2013-10-30 17:08:11

标签: java android android-fragments

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

3 个答案:

答案 0 :(得分:0)

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

todoItems = savedInstanceState.getStringArrayList("todoItemTag");

我建议您测试数组以确保它不为null。类似的东西:

Log.i("ARRAY",todoItems.length);

答案 1 :(得分:0)

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

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

}

答案 2 :(得分:0)

移动此行

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

进入你的其他声明。

由于片段管理器找不到您刚刚提交的片段,因此您在第一次运行时基本上将listToDo置零。 commit()计划要添加的片段。这并不意味着它们会立即添加。因此,当您立即调用findFragmentById时,调用listToDo将返回null,这就是{{1}}结束为空的原因。