1

我有一個活動,它有一個片段。當我點擊一個列表項目時,當前片段被替換爲另一個片段。這用於正常工作...ListFragment從SupportFragmentManager切換到FragmentManager後替換「AdapterView(View)不支持在AdapterView中」

但是,我想從SupportFragmentManager移到FragmentManager ...我將不再支持舊的Android版本。

當我開關我的應用程序開始崩潰與上述錯誤。這是一個完整的堆棧跟蹤...

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) 

這是我做的唯一的變化,現在我真的很困惑。

這裏是他們的代碼鍵片段:

活動:

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:

活動:

<?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> 

感謝您的幫助,讓我知道,如果你需要更多的信息或細節。

+0

不要替換放置在xml佈局中的碎片。改爲使用容器並以編程方式將它們添加到該容器。 – Luksprog 2013-05-06 07:42:51

+0

@Luksprog的建議幫助解決了第一個問題。通過創建容器,我能夠用第二個替換第一個片段。但是,當我點擊後退按鈕並從後臺返回FragmentA時,我現在得到相同的錯誤。有什麼建議麼? – AlexIIP 2013-05-06 15:45:38

+1

只是爲了好奇心,你可以用另一個佈局('FrameLayout','RelativeLayout'等)包裝ListView嗎?看看它是怎麼回事? – Luksprog 2013-05-06 16:18:47

回答

3

要在事務中使用的片段不應放置在xml佈局中。應該使用容器佈局,並將碎片添加到該容器中。

爲了避免addView()例外包裝您ListView在另一個佈局,我不知道爲什麼發生這種情況,但它似乎在框架調用addView()方法(不是爲AdapterView的兒童實現)的片段它被重建時。

+0

我不是很確定,但在閱讀這個答案後,我玩了一點點的XML並從我的主容器中移除了這行xmlns:tools = http://schemas.android.com/tools(android.support.v4 .widget.DrawerLayout),然後它爲我工作。 – Wak 2013-11-25 09:22:51

0

堆棧跟蹤告訴你有一個AdapterView錯誤。看看已知的子類,我從你的帖子看到的唯一一個AdapterView就是你的ListView。確保你沒有試圖調用addView(View)。

+0

我沒有在任何地方調用addView ...請看看我對上面的@Luksprog做出的迴應。 – AlexIIP 2013-05-06 15:49:41

0

只是要清楚,我也只好說讓我stucked幾個小時了同樣的問題...

我有一個列表視圖,而不是一個的LinearLayout,並得到了我很多麻煩,

一旦我替換爲LinearLayout一切正常