原因:android.view.InflateException: Binary XML file line #11: Error inflating class fragment

Posted

技术标签:

【中文标题】原因:android.view.InflateException: Binary XML file line #11: Error inflating class fragment【英文标题】:Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment 【发布时间】:2014-01-19 04:51:39 【问题描述】:

我正在尝试使用 Fragments 构建一个应用程序,它处于初学者级别,并且我在该主题上没有直接的扎实知识或经验。

我希望 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 ComponentInfocom.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_
    android:layout_
    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_/>

</LinearLayout>

【问题讨论】:

【参考方案1】:
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  

【讨论】:

谢谢!但是当我添加@Override 时,Eclipse 给了我“WebViewFragment 类型的 onCreateView(LayoutInflater, ViewGroup, Bundle) 方法必须覆盖或实现超类型方法”:/ @user2734182 它只是一个注释,表明它是一个被覆盖的方法。没有它它应该可以工作 "引起:java.lang.ClassCastException: com.raj.fragment.MainActivity 无法转换为android.app.Activity" @user2734182 发布您更新的代码,包括活动代码和片段代码以及导入语句 现在可以使用 =) 或者是的,应用程序启动但为空白/白色:S【参考方案2】:

WebViewFragment 不应该是 FragmentActivity。既然是fragment,就应该继承Fragment。

public class WebViewFragment extends Fragment

【讨论】:

以上是关于原因:android.view.InflateException: Binary XML file line #11: Error inflating class fragment的主要内容,如果未能解决你的问题,请参考以下文章

普通原因与特殊原因的区别

rca根本原因分析法是啥意思

ip、端口 连接失败问题常见原因

在MYSQL里 段错误 是啥原因

“'NSInternalInconsistencyException' 的替代原因,原因:'此 NSPersistentStoreCoordinator 没有持久存储。'”

Oracle连接太慢的原因及TNS无监听程序