我正在使用带有操作栏选项卡的viewpager,我在布局中有两个片段,问题是我得到 class-cast-exception
我的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();
}
}
唯一不同的是正在调用的Detail片段类。
我做错了吗?
还有什么我们需要照顾的。
答案 0 :(得分:0)
好吧,我解决了这个问题......解决方案有点奇怪,但对我有用。
我决定使用常见的detail-fragment类,而不是为每个选项卡分别使用片段类。并根据当前当前选择显示内容。