android.view.InflateException - 找不到原因

时间:2017-07-07 17:30:50

标签: android android-fragments

这是我的片段inputtext_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inputtextfragm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/inputtext_layout"
        android:visibility="invisible"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        ...>

        ...

    </LinearLayout>
</fragment>

......和:

package de.company.android.inputtext;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class InputTextFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        try {
            return inflater.inflate(R.layout.inputtext_fragment, container, false);
        }
        catch (Throwable t) {
            t.printStackTrace();
            return null;
        }
    }
}

这是活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    tools:context="de.company.android.testview.MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">

    ...

    <fragment
        android:id="@+id/console"
        android:name="de.company.android.inputtext.InputTextFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

当活动的内容视图设置为onCreate()时,我在启动时收到以下错误:

D / AndroidRuntime:关闭VM E / AndroidRuntime:致命异常:主要                   过程:de.company.android.testview,PID:573                   java.lang.RuntimeException:无法启动活动ComponentInfo {de.company.android.testview / de.company.android.testview.MainActivity}:android.view.InflateException:二进制XML文件行#153:二进制XML文件行#153 :错误膨胀类片段                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)                       在android.app.ActivityThread.access $ 1100(ActivityThread.java:229)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1821)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:148)                       在android.app.ActivityThread.main(ActivityThread.java:7325)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

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

引起:java.lang.IllegalStateException:Fragment de.company.android.inputtext.InputTextFragment没有创建视图。                       在android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2253)                       在android.app.FragmentController.onCreateView(FragmentController.java:99)                       在android.app.Activity.onCreateView(Activity.java:6085)                       在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)                       在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)                       在android.view.LayoutInflater.rInflate(LayoutInflater.java:847)                       在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)                       在android.view.LayoutInflater.inflate(LayoutInflater.java:527)                       在android.view.LayoutInflater.inflate(LayoutInflater.java:429)                       在android.view.LayoutInflater.inflate(LayoutInflater.java:380)                       在com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:479)                       在android.app.Activity.setContentView(Activity.java:2400)                       在de.company.android.testview.MainActivity.onCreate(MainActivity.java:1697)                       在android.app.Activity.performCreate(Activity.java:6904)                       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)                       在android.app.ActivityThread.access $ 1100(ActivityThread.java:229)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1821)                       在android.os.Handler.dispatchMessage(Handler.java:102)                       在android.os.Looper.loop(Looper.java:148)                       在android.app.ActivityThread.main(ActivityThread.java:7325)                       at java.lang.reflect.Method.invoke(Native Method)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是catch中记录的错误:

W/System.err: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class fragment
W/System.err:     at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
W/System.err:     at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
W/System.err:     at de.company.android.inputtext.InputTextFragment.onCreateView(InputTextFragment.java:23)
W/System.err:     at android.app.Fragment.performCreateView(Fragment.java:2281)
W/System.err:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
W/System.err:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1142)
W/System.err:     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1246)
W/System.err:     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2223)
W/System.err:     at android.app.FragmentController.onCreateView(FragmentController.java:99)
W/System.err:     at android.app.Activity.onCreateView(Activity.java:6085)
W/System.err:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
W/System.err:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
W/System.err:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
W/System.err:     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
W/System.err:     at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
...
W/System.err: Caused by: java.lang.NullPointerException
W/System.err:     at java.lang.VMClassLoader.findLoadedClass(Native Method)
W/System.err:     at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
W/System.err:     at android.app.Fragment.instantiate(Fragment.java:620)
W/System.err:     at android.app.Fragment.instantiate(Fragment.java:598)
W/System.err:     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2214)
W/System.err:     at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:192)
W/System.err:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:766)
W/System.err:   ... 33 more

我无法找出问题所在。活动和片段位于不同的模块中,但所有内容都配置正确,以便正确引用de.company.android.inputtext util。

3 个答案:

答案 0 :(得分:1)

从inputtext_fragment.xml布局

中删除片段
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/inputtext_layout"
        android:visibility="invisible"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        ...>

        ...

</LinearLayout>

答案 1 :(得分:1)

您的input_text_fragment.xml不应位于Fragment标记内。它应该在适当类型的布局内。片段标记仅在main_activity.xml中,您希望片段出现在其中。

答案 2 :(得分:0)

引起:java.lang.IllegalStateException:Fragment de.company.android.inputtext.InputTextFragment没有创建视图。

您无法在onCreateView中返回空视图。