引起:android.view.InflateException:二进制XML文件行#11:错误膨胀类片段

时间:2014-01-01 17:16:50

标签: android android-fragments fragment android-fragmentactivity

我正在尝试使用片段构建应用程序,这些片段处于初级水平,而且我对该主题没有直接的扎实知识或经验。

我希望在应用程序启动时可以看到WebViewFragment,之后再添加ListView端。

现在,应用程序在手机上启动后立即崩溃。我已粘贴下面的错误日志。

错误

01-01 18:14:24.223: E/AndroidRuntime(25050): FATAL EXCEPTION: main
01-01 18:14:24.223: E/AndroidRuntime(25050): Process: com.raj.fragment, PID: 25050
01-01 18:14:24.223: E/AndroidRuntime(25050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raj.fragment/com.raj.fragment.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.os.Looper.loop(Looper.java:136)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.ActivityThread.main(ActivityThread.java:5050)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at java.lang.reflect.Method.invokeNative(Native Method)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at java.lang.reflect.Method.invoke(Method.java:515)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at dalvik.system.NativeStart.main(Native Method)
01-01 18:14:24.223: E/AndroidRuntime(25050): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.Activity.setContentView(Activity.java:1929)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at com.raj.fragment.MainActivity.onCreate(MainActivity.java:11)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.Activity.performCreate(Activity.java:5231)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
01-01 18:14:24.223: E/AndroidRuntime(25050):    ... 11 more
01-01 18:14:24.223: E/AndroidRuntime(25050): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.raj.fragment.WebViewFragment that is not a Fragment
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.Fragment.instantiate(Fragment.java:585)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.Fragment.instantiate(Fragment.java:561)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.app.Activity.onCreateView(Activity.java:4778)
01-01 18:14:24.223: E/AndroidRuntime(25050):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
01-01 18:14:24.223: E/AndroidRuntime(25050):    ... 21 more
01-01 18:14:24.223: E/AndroidRuntime(25050): Caused by: java.lang.ClassCastException
01-01 18:14:24.223: E/AndroidRuntime(25050):    ... 25 more

MainActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
}

WebViewFragment

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("SetJavaScriptEnabled")
public class WebViewFragment extends Fragment {

    private WebView mWebView;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.webview, container, false);

        mWebView = (WebView)view.findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com/about/");
        mWebView.setWebViewClient(new WebClient());

        return view;    
    }

    public class WebClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
            webview.loadUrl(url);
            return true;
        }
    }
}

activity_main.xml中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


       <fragment 
            android:name="com.raj.fragment.WebViewFragment"
            android:id="@+id/webview"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:4)

Trying to instantiate a class com.raj.fragment.WebViewFragment that is not a Fragment

日志说明了一切

更改为

public class WebViewFragment extends Fragment {

因为你有

<fragment android:name="com.raj.fragment.WebViewFragment" // is a fragment

您的FragmentMainActivity托管。

您还可以拥有class="com.raj.fragment.WebViewFragment"android:name="com.raj.fragment.WebViewFragment"。不需要两者都有。

同时添加@Override注释

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,

编辑:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);// missing  
}

答案 1 :(得分:0)

WebViewFragment不应该是FragmentActivity。由于它是一个片段,它应该继承片段。

public class WebViewFragment extends Fragment