在xml代码中声明片段时的ClassCastException

时间:2012-11-17 06:18:40

标签: android fragment android-support-library

我在布局中声明了一个Fragment然后尝试给该视图充气,但我总是得到ClassCastException。请帮我解决这个问题。

这是我的布局来源:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/fillparent"
    android:background="#ffffffff"
    android:orientation="vertical" >

    <android.support.v4.app.Fragment
        android:name="com.ihnel.tinyapp.xskt.view.ResultView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="55dp" />

    <ihnel.lib.topbar.TopBar
        android:id="@+id/container_topbar"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/topbar_header" >
    </ihnel.lib.topbar.TopBar>

</RelativeLayout>

这是抛出异常的java代码:

container = (ViewGroup) LayoutInflater.from(getApplicationContext()).inflate(R.layout.container, null);

这是logcat:

11-17 13:17:34.689: E/AndroidRuntime(21674): FATAL EXCEPTION: main
11-17 13:17:34.689: E/AndroidRuntime(21674): java.lang.RuntimeException: Unable to start activity ComponentInfo{ihnel.tinyapp.xskt/com.ihnel.tinyapp.xskt.Home}: android.view.InflateException: Binary XML file line #7: Class is not a View android.support.v4.app.Fragment
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1996)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2023)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.os.Looper.loop(Looper.java:137)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.ActivityThread.main(ActivityThread.java:4503)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at java.lang.reflect.Method.invokeNative(Native Method)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at java.lang.reflect.Method.invoke(Method.java:511)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at dalvik.system.NativeStart.main(Native Method)
11-17 13:17:34.689: E/AndroidRuntime(21674): Caused by: android.view.InflateException: Binary XML file line #7: Class is not a View android.support.v4.app.Fragment
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.view.LayoutInflater.createView(LayoutInflater.java:599)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:741)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.view.LayoutInflater.inflate(LayoutInflater.java:491)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:262)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.Activity.setContentView(Activity.java:1846)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at com.ihnel.tinyapp.xskt.Home.onCreate(Home.java:28)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.Activity.performCreate(Activity.java:4504)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
11-17 13:17:34.689: E/AndroidRuntime(21674):    ... 11 more
11-17 13:17:34.689: E/AndroidRuntime(21674): Caused by: java.lang.ClassCastException: android.support.v4.app.Fragment cannot be cast to android.view.View
11-17 13:17:34.689: E/AndroidRuntime(21674):    at java.lang.Class.asSubclass(Class.java:1380)
11-17 13:17:34.689: E/AndroidRuntime(21674):    at android.view.LayoutInflater.createView(LayoutInflater.java:554)
11-17 13:17:34.689: E/AndroidRuntime(21674):    ... 22 more
11-17 13:17:34.690: E/IBINDER_HOOK(21674): open whitelist file error

这是ResultView片段:

   package com.ihnel.tinyapp.xskt.view;

import ihnel.tinyapp.xskt.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

This is the ResultView fragment:

    public class ResultView extends Fragment {

        /** The webview which is used to show result. */
        WebView webview_main;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.resultview, null);
            init(view);
            return view;
        }

        /**
         * Inits the fragment view.
         *
         * @param view the root view.
         */
        private void init(View view){
                }
    }

2 个答案:

答案 0 :(得分:0)

似乎com.ihnel.tinyapp.xskt.view.ResultView不是片段! Docs

答案 1 :(得分:0)

像这样编辑你的布局xml。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
**xmlns:tools="http://schemas.android.com/tools"**
style="@style/fillparent"
android:background="#ffffffff"
android:orientation="vertical" >

我遇到了同样的问题。我无法通过任何建议的解决方案解决这个问题。当我像这样编辑我的布局xml时,问题就神奇地消失了。我仍然不知道为什么问题永远不会再发生,即使我删除了这行,xmlns:tools =“http://schemas.android.com/tools”。