Fragment 和 ViewPager ClassCastException
我正在使用带有操作栏选项卡的 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 片段类。
我做错了吗?
还有什么需要我们照顾的吗?
I am using viewpager with action-bar tab and I am have two fragment in layout, and the problem is I am getting class-cast-exception
my xml file is look like below
<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>
and I have two tabs in my activity songs and album.
It loads correctly in portrait mode but in, landscape mode it shows class cast exception.
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)
error is shown in here
in AlbumFragMent class code for dual mode is
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();
}
}
and in SongFragment
class
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();
}
}
only difference in both is the Detail fragment class which is calling.
Am I doing it wrong?
Is there anything else we need to take care of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我解决了这个问题...这个解决方案有点奇怪,但对我有用。
我决定使用通用的细节片段类,而不是为每个选项卡使用单独的片段类。并根据当前当前选择显示内容。
okay i solve this... the solution is kind of weird but works for me.
I have decides to go with common detail-fragment class rather then having separate fragment class for each tab. and display the content based on the current current selection.