无法强制转换为android.app.Fragment

时间:2012-04-30 11:51:28

标签: android

我只是想看看Fragment,我得到了一些愚蠢的东西...... 有我的SkippersActivity.java:

public class SkippersActivity extends Activity{
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.skippers_fragment);
 }
}

我的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>

还有我的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;
}   

}

我的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>

为什么我得到:

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.access$600(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)

4 个答案:

答案 0 :(得分:101)

如果我不得不猜测,SkippersFragment正在延长android.support.v4.app.Fragment。但是,SkippersActivity 扩展android.support.v4.app.FragmentActivity。因此SkippersActivity正在尝试使用本机API Level 11片段。

答案 1 :(得分:19)

如果您使用v4兼容包,则您的Activity必须扩展FragmentActivity,而不是Activity。如果你使用Android&gt; 3.0,然后你的Activity扩展了Activity,但阅读了CommonsWare关于不混合两种类型片段的答案。

答案 2 :(得分:2)

要注意的另一个缺陷是确保你不要忘记从你的活动中调用super.onCreate()。如果你像我一样犯这个愚蠢的错误,你也会看到“错误膨胀类片段”和“...无法转换为android.app.Fragment”错误,即使你没有错配标准和支持.v4版本的类。

答案 3 :(得分:0)

我在尝试扩展ListFragment时遇到了同样的错误。我的发现是,在具有minSdkVersion&lt;的项目上使用ListFragment。 11,我将import android.support.v4.app.ListFragment;。否则,我将不得不import android.app.ListFragment;

link澄清了我的问题。