cgbs cgbs - 11 days ago 5
Android Question

How can I get rid of the "null pointer exception" while creating fragments dynamically?

I want to do to-do list application by using fragments. While creating this fragments dynamically, I take a null pointer exception at this line (in

mainactivity.java
):

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


I cannot see this message on logcat: Log.w("6", "altı");
What is wrong with my code? How can I solve this problem?

The
activity_main.xml
file:

<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

Answer

Move this line

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

into your else statement.

You are essentially nulling out listToDo on your first run since the fragment manager will not find your fragment that you just committed. commit() schedules your fragments to be added. It does not mean they are added right away. So, calling findFragmentById will return null when you call it immediately after which is why listToDo ends up null.