ClassCastException异常:CustomFragment不能转换为android.app.Fragment /使用android.support.v4.app.Fragment [英] ClassCastException: CustomFragment cannot be cast to android.app.Fragment / Using android.support.v4.app.Fragment

查看:527
本文介绍了ClassCastException异常:CustomFragment不能转换为android.app.Fragment /使用android.support.v4.app.Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的一个库和两个应用程序分为申请:1的手机,其他的平板电脑。很多code和布局库中定义,只有少数部分定义的应用程序。

I am developing an application divided in one library and two applications : 1 for phones, the other for tablets. A lot of code and layout is defined in the library, and only a few parts are defined in the applications.

,用 ViewPager 的手机版本(布局和活动定义我使用的片段在活动在图书馆,用于在手机上的应用程序没有变化​​)。 对于平板电脑的版本,我想告诉我的片段(2)并排,而不是在一个 ViewPager ,所以我试图建立一个这样的XML布局:

I'm using fragments in an Activity, with a ViewPager for the phone version (layout and activity defined in the library, used with no change in the phone application). For the tablet version, I want to show my fragments (2) side by side, and not in a ViewPager, so I tried to build an XML layout like that :

<....>
    <LinearLayout
        android:id="@+id/fragmentsParent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/f1"
            android:class="com.test.Fragment1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <fragment
            android:id="@+id/f2"
            android:name="com.test.Fragment2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"  />
    </LinearLayout>
</....>

我的问题是,code正在我的手机上的应用程序,而不是在我的平板电脑的应用程序。在这其中,我得到这个异​​常堆栈:

My problem is that the code is working on my phone application, but not on my tablet application. On this one, I get this exception stack :

07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)
07-11 17:41:14.032: E/AndroidRuntime(14754):    ... 11 more

07-11 17:41:14.032: E/AndroidRuntime(14754): Caused by: java.lang.ClassCastException: com.test.Fragment1 cannot be cast to android.app.Fragment

07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Fragment.instantiate(Fragment.java:560)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Fragment.instantiate(Fragment.java:535)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.app.Activity.onCreateView(Activity.java:4168)
07-11 17:41:14.032: E/AndroidRuntime(14754):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)

该错误消息谈谈 android.app.Fragment 其中,我希望 android.support.v4.app.Fragment

在网上搜索后,我检查以下几点:

After searching the web, I checked these points :

  • 在我的活动来延长 android.support.v4.app.FragmentActivity
  • 在我的片段延长 android.support.v4.app.Fragment
  • 在我的应用程序使用support.v4库。
  • My activity extends android.support.v4.app.FragmentActivity,
  • My fragments extend android.support.v4.app.Fragment,
  • My application uses the support.v4 library.

推荐答案

您已经活动由FragmentActivity延长。

Your Activity have to be extended by FragmentActivity.

这篇关于ClassCastException异常:CustomFragment不能转换为android.app.Fragment /使用android.support.v4.app.Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆