如何将我的 Activity 升级为 Fragments 并避免 ClassCastException?
我有一个相当大的活动类层次结构。我刚刚安装了 v4 兼容包。获取所有 Activity 的最简单方法是什么,以便我可以在其中包含 TabHost 的 Fragment 中使用它们?
代码行:
mTabsAdapter.addTab(mTabHost.newTabSpec("feed").setIndicator("Feed"), FActivity.class, null);
第 165 行:
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
具体来说,我想避免以下错误:
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): java.lang.ClassCastException: com.c.FActivity
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): at android.support.v4.app.Fragment.instantiate(Fragment.java:378)
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): at com.c.Main$TabsAdapter.getItem(Main.java:165)
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:62)
I have quite a large hierarchy of classes that are Activities. I just installed the v4 compatibility package. What is the easiest way to get all my Activities in a way that I can use them in a Fragment with a TabHost inside of it?
Line of code:
mTabsAdapter.addTab(mTabHost.newTabSpec("feed").setIndicator("Feed"), FActivity.class, null);
Line 165:
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
Specifically I want to avoid errors like:
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): java.lang.ClassCastException: com.c.FActivity
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): at android.support.v4.app.Fragment.instantiate(Fragment.java:378)
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): at com.c.Main$TabsAdapter.getItem(Main.java:165)
07-19 01:33:24.940: ERROR/AndroidRuntime(17581): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:62)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是一些您可能会感兴趣的链接。有一个创建两个活动应用程序,然后在其中实现片段的示例。
Android 用户界面设计:使用片段
Android 兼容性:使用片段
希望这会有所帮助。
Here are a few links that you might find interesting. There is an example of creating a two-activity application, and then implementing fragments into it.
Android User Interface Design: Working With Fragments
Android Compatibility: Working with Fragments
Hope this helps.