无法投射到 android.app.Fragment
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10383247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot be cast to android.app.Fragment
提问by Arkezis
I'm just trying to have a look on Fragment and I got some probs with stupid stuff... There is my SkippersActivity.java :
我只是想看看 Fragment 并且我遇到了一些愚蠢的问题......这是我的 SkippersActivity.java :
public class SkippersActivity extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.skippers_fragment);
}
}
And my skippers_fragment.xml :
而我的 skippers_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.arkezis.globedroid.SkippersFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/skippers_fragment">
</fragment>
And there is my SkippersFragment.xml :
还有我的 SkippersFragment.xml :
public class SkippersFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View mainView = inflater.inflate(R.layout.skippers, container, false);
return mainView;
}
}
}
My skippers.xml :
我的 skippers.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView android:id="@+id/list_skippers_all"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
Why do I got :
为什么我得到:
04-30 13:45:16.355: E/AndroidRuntime(30077): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arkezis.globedroid/com.arkezis.globedroid.SkippersActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.ActivityThread.access0(ActivityThread.java:123)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.os.Looper.loop(Looper.java:137)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-30 13:45:16.355: E/AndroidRuntime(30077): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 13:45:16.355: E/AndroidRuntime(30077): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 13:45:16.355: E/AndroidRuntime(30077): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 13:45:16.355: E/AndroidRuntime(30077): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 13:45:16.355: E/AndroidRuntime(30077): at dalvik.system.NativeStart.main(Native Method)
04-30 13:45:16.355: E/AndroidRuntime(30077): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-30 13:45:16.355: E/AndroidRuntime(30077): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.Activity.setContentView(Activity.java:1835)
04-30 13:45:16.355: E/AndroidRuntime(30077): at com.arkezis.globedroid.SkippersActivity.onCreate(SkippersActivity.java:19)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.Activity.performCreate(Activity.java:4465)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-30 13:45:16.355: E/AndroidRuntime(30077): ... 11 more
04-30 13:45:16.355: E/AndroidRuntime(30077): Caused by: java.lang.ClassCastException: com.arkezis.globedroid.SkippersFragment cannot be cast to android.app.Fragment
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.Fragment.instantiate(Fragment.java:574)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.Fragment.instantiate(Fragment.java:549)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.app.Activity.onCreateView(Activity.java:4235)
04-30 13:45:16.355: E/AndroidRuntime(30077): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
回答by CommonsWare
If I had to guess, SkippersFragment
is extending android.support.v4.app.Fragment
. However, SkippersActivity
is notextending android.support.v4.app.FragmentActivity
. So SkippersActivity
is trying to use native API Level 11 fragments.
如果我不得不猜测,SkippersFragment
是扩展android.support.v4.app.Fragment
. 然而,SkippersActivity
在不延长android.support.v4.app.FragmentActivity
。因此,SkippersActivity
正在尝试使用原生API等级11片段。
回答by Christine
If you use the v4 compatibility package, your Activity has to extend FragmentActivity, not Activity. If you use Android > 3.0, then your Activity extends Activity, but read CommonsWare's answer about not mixing both types of fragments.
如果您使用 v4 兼容包,您的 Activity 必须扩展 FragmentActivity,而不是 Activity。如果您使用 Android > 3.0,那么您的 Activity 将扩展 Activity,但请阅读 CommonsWare 关于不要混合两种类型片段的答案。
回答by dfinn
Another pitfall to watch out for is to make sure you don't forget to call super.onCreate() from your activity. If you make this dumb mistake like I did, you'll also see the "Error inflating class fragment" and "... cannot be cast to android.app.Fragment" errors, even if you haven't mismatched the standard and support.v4 versions of the classes.
另一个需要注意的陷阱是确保您不要忘记从您的活动中调用 super.onCreate()。如果你像我一样犯了这个愚蠢的错误,你也会看到“错误膨胀类片段”和“...无法转换为 android.app.Fragment”错误,即使你没有不匹配标准和支持.v4 版本的类。
回答by Dino Tw
I ran into the same error while trying to extend ListFragment. My finding is that, to use ListFragment on a project with minSdkVersion < 11, I will have to import android.support.v4.app.ListFragment;
. Otherwise, I will have to import android.app.ListFragment;
.
我在尝试扩展 ListFragment 时遇到了同样的错误。我的发现是,要在 minSdkVersion < 11 的项目上使用 ListFragment,我必须使用import android.support.v4.app.ListFragment;
. 否则,我将不得不import android.app.ListFragment;
。
This linkclarified my question.
这个链接澄清了我的问题。