带有视图寻呼机的 Tabhost,ClassCast 错误

Posted

技术标签:

【中文标题】带有视图寻呼机的 Tabhost,ClassCast 错误【英文标题】:Tabhost with view pager, ClassCast error 【发布时间】:2014-10-17 05:29:20 【问题描述】:

我有一个活动,它为一周中的每一天填充滚动视图,并使用视图寻呼机在操作栏上制作的选项卡显示它们。每当我回到我已经访问过的选项卡时,我都会收到一个类转换错误。 这是我的日志猫。谁能告诉我为什么会导致这个异常。

08-23 05:01:50.589: E/androidRuntime(3836): FATAL EXCEPTION: main

08-23 05:01:50.589:E/AndroidRuntime(3836):java.lang.ClassCastException:android.view.AbsSavedState$1 cannot be cast to android.widget.CompoundButton$SavedState
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.widget.CompoundButton.onRestoreInstanceState(CompoundButton.java:379)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.View.dispatchRestoreInstanceState(View.java:12284)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2626)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2626)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2626)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2626)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2626)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.View.restoreHierarchyState(View.java:12262)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.app.Fragment.restoreViewState(Fragment.java:425)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:949)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:550)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:509)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:490)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at com.example.attendogram.Start.onTabSelected(Start.java:195)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:572)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1098)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.View.performClick(View.java:4240)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.view.View$PerformClick.run(View.java:17721)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.os.Handler.handleCallback(Handler.java:730)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.os.Looper.loop(Looper.java:137)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at java.lang.reflect.Method.invoke(Method.java:525)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 05:01:50.589: E/AndroidRuntime(3836):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

【参考方案1】:

我遇到了类似的问题。在寻找答案时,我遇到了这个帖子。 Unexplainable ClassCastException in android.widget.ProgressBar.onRestoreInstanceState

在我们所有的案例中,android.view.AbsSavedState$1 都被强制转换为其他组件。

接受的答案说问题在于应用程序正在重用 id。

“仔细检查您的应用程序是否在两个不同的地方重复使用相同的 ID”

如果您使用的是 eclipse,Lint 工具有助于追踪重复的 id。 。我希望这有帮助。

【讨论】:

以上是关于带有视图寻呼机的 Tabhost,ClassCast 错误的主要内容,如果未能解决你的问题,请参考以下文章

带有自动滚动指示器的 Android 视图寻呼机

BUG recyclerview 中的某些行在平板电脑的视图寻呼机内不可点击

如何从使用视图寻呼机创建的选项卡片段转移到另一个片段

刷新视图寻呼机选项卡的内容

如何将活动中的searchview的输入发送到该活动的视图寻呼机中的片段

ViewPager 页面效果