1

我使用viewpager用行動吧標籤和我有佈局兩個碎片,問題是我得到類強制轉換的異常片段和ViewPager ClassCastException異常

我的XML文件看起來像下面

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="2.5" /> 

     <FrameLayout 
      android:id="@+id/fragment_details" 
      android:layout_width="0px" 
      android:layout_height="match_parent" 
      android:layout_weight="2" /> 
    </LinearLayout> 

我在我的活動歌曲和專輯中有兩個選項卡。

它在縱向模式下正確加載,但在橫向模式下顯示類別轉換異常。示於此處

12-08 04:30:08.198: E/AndroidRuntime(2166): FATAL EXCEPTION: main 
12-08 04:30:08.198: E/AndroidRuntime(2166): java.lang.ClassCastException: com.nix.music.activity.fragments.SongDetailsFragment 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at com.nix.music.activity.fragments.AlbumFragMent.showDetails(AlbumFragMent.java:81) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at com.nix.music.activity.fragments.AlbumFragMent.access$0(AlbumFragMent.java:77) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at com.nix.music.activity.fragments.AlbumFragMent$1.onItemClick(AlbumFragMent.java:52) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at android.os.Handler.handleCallback(Handler.java:587) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at android.os.Looper.loop(Looper.java:123) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at java.lang.reflect.Method.invoke(Method.java:521) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-08 04:30:08.198: E/AndroidRuntime(2166):  at dalvik.system.NativeStart.main(Native Method) 

誤差

在雙模AlbumFragMent類代碼

if (mDualPane) { 
    AlbumDetailFragment details = (AlbumDetailFragment) getFragmentManager() 
      .findFragmentById(R.id.fragment_details); 
    if (details == null || details.getShownIndex() != index) { 
     // Make new fragment to show this selection. 
     details = AlbumDetailFragment.newInstance(index); 

     // Execute a transaction, replacing any existing fragment 
     // with this one inside the frame. 
     FragmentTransaction ft = getFragmentManager() 
       .beginTransaction(); 
     ft.replace(R.id.fragment_details, details); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
     ft.commit(); 

    } 
} 

SongFragment

if (mDualPane) { 
    getListView().setItemChecked(index, true); 

    SongDetailsFragment details = (SongDetailsFragment) getFragmentManager() 
      .findFragmentById(R.id.fragment_details); 
    if (details == null || details.getShownIndex() != index) { 
     // Make new fragment to show this selection. 
     details = SongDetailsFragment.newInstance(index); 

     // Execute a transaction, replacing any existing fragment 
     // with this one inside the frame. 
     FragmentTransaction ft = getFragmentManager() 
       .beginTransaction(); 
     ft.replace(R.id.fragment_details, details); 
     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
     ft.commit(); 
    } 
} 

僅在兩個不同的是詳細信息片段正在呼叫的班級。

我做錯了嗎?

還有什麼我們需要照顧的。

回答

0

好吧我解決了這個問題...解決方案有點奇怪,但爲我工作。

我決定使用通用的detail-fragment類,而不是爲每個選項卡分別使用不同的片段類。並根據當前的當前選擇顯示內容。