1

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.

4

3 回答 3

3

将在事务中使用的片段不应放置在 xml 布局中。相反,容器布局应该与添加到该容器的片段一起使用。

为了避免将addView()异常包装ListView在另一个布局中,我不知道为什么会发生这种情况,但似乎框架在重建片段时调用了addView()(未为 的子级实现)方法。AdapterView

于 2013-05-07T04:01:21.547 回答
0

为了清楚起见,我也遇到了同样的问题,让我被困了几个小时......

我有一个 Listview 而不是一个 LinearLayout,这给我带来了很多麻烦,

一旦我更换了 LinearLayout 一切都很好

于 2013-05-31T10:02:15.880 回答
0

堆栈跟踪告诉您AdapterView存在错误。查看已知的子类,我从您的帖子中看到的唯一一个 AdapterView 就是您的 ListView。确保您没有尝试调用 addView(View) 。

于 2013-05-06T07:47:59.110 回答