I have an activity which has a fragment. When I click on a list item the current fragment gets replaced with another fragment. This used to work correctly...
However, I would like to move away from SupportFragmentManager to FragmentManager...I will no longer support old android versions.
When I made the switch my app started crashing with the above error. Here is a full stacktrace...
05-06 05:24:35.868: E/AndroidRuntime(1381): FATAL EXCEPTION: main
05-06 05:24:35.868: E/AndroidRuntime(1381): java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.widget.AdapterView.addView(AdapterView.java:445)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:839)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.app.BackStackRecord.run(BackStackRecord.java:622)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.os.Handler.handleCallback(Handler.java:605)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.os.Looper.loop(Looper.java:137)
05-06 05:24:35.868: E/AndroidRuntime(1381): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-06 05:24:35.868: E/AndroidRuntime(1381): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 05:24:35.868: E/AndroidRuntime(1381): at java.lang.reflect.Method.invoke(Method.java:511)
05-06 05:24:35.868: E/AndroidRuntime(1381): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-06 05:24:35.868: E/AndroidRuntime(1381): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-06 05:24:35.868: E/AndroidRuntime(1381): at dalvik.system.NativeStart.main(Native Method)
This is the only change that I made and now I am really confused.
Here are they key snippets of code:
Activity:
Fragment2 mFragment2 = new Fragment2();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.listFragment1, mFragment2 , FRAGMENT2_FRAGMENT_TAG);
transaction.addToBackStack(null);
transaction.commit();
Fragment2:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragment2= inflater.inflate(R.layout.list_fragment2,
container, false);
return fragment2;
}
XML:
Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/lists_background"
android:divider="@color/black">
<TextView
android:id="@+id/list_header_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="@color/black"
android:textStyle="bold"/>
<fragment
android:id="@+id/listFragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/list_header_text"
android:layout_centerHorizontal="true"
class="com.test.Fragment1"
android:tag="Fragment1" />
</RelativeLayout>
Fragment2:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/list"
android:listSelector="@android:color/transparent"
android:background="@drawable/lists_background"
android:divider="@color/black"
android:dividerHeight="1dip"
android:clickable="true">
</ListView>
Thank you for your help and let me know if you need more info or details.