包含布局时如何解决 inflateException 问题?

Posted

技术标签:

【中文标题】包含布局时如何解决 inflateException 问题?【英文标题】:How to resolve problem of inflateException when I include a layout? 【发布时间】:2020-08-24 09:47:12 【问题描述】:

我设计了一个新的表单名称 (form_info.xml) 以从用户那里获取一些信息,然后我尝试将表单包含到 activity_main.xml 但不幸的是应用程序崩溃了并且我得到了 inflateExcdeption 但我不知道如何处理它。此外,当我通过 setContentView (R.layout.form_info) 添加新表单时,我遇到了同样的错误。

package com.google.a05_layout_decoration_design_form_information;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity 

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
    

有activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:context=".MainActivity">

    <include layout="@layout/form_info"/>

</RelativeLayout>

下面是form_info.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_form_info"
    android:orientation="vertical"
    android:layout_
    android:layout_
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="horizontal">

        <EditText
            android:id="@+id/input_name"
            android:layout_
            android:layout_
            android:ems="10"
            android:hint="Name"
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/input_phone"
            android:layout_
            android:layout_
            android:ems="10"
            android:hint="Phone No."
            android:inputType="phone" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/cb_share_phone"
        android:layout_
        android:layout_
        android:text="Share my phone number"
        android:checked="true"/>

    <view
        android:layout_
        android:layout_
        android:background="#000000"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

    <LinearLayout
        android:layout_
        android:layout_
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_
            android:layout_
            android:layout_weight="0"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/email_icon" />

        <EditText
            android:id="@+id/input_email"
            android:layout_
            android:layout_
            android:ems="10"
            android:hint="Email"
            android:inputType="textEmailAddress" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_submit"
        android:layout_
        android:layout_
        android:layout_weight="0"
        android:text="Submit"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="150dp"
        android:layout_marginRight="150dp"/>

</LinearLayout>

而Logcat是:

05-08 22:43:36.953 15798-15798/? I/art: Not late-enabling -Xcheck:jni (already on)
05-08 22:43:37.028 15798-15798/com.google.a05_layoutdecoration_designforminformation W/System: ClassLoader referenced unknown path: /data/app/com.google.a05_layoutdecoration_designforminformation-1/lib/x86
05-08 22:43:37.094 15798-15798/com.google.a05_layoutdecoration_designforminformation W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-08 22:43:37.134 15798-15798/com.google.a05_layoutdecoration_designforminformation I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-08 22:43:37.135 15798-15798/com.google.a05_layoutdecoration_designforminformation I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
05-08 22:43:37.312 15798-15798/com.google.a05_layoutdecoration_designforminformation D/AndroidRuntime: Shutting down VM
05-08 22:43:37.312 15798-15798/com.google.a05_layoutdecoration_designforminformation E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.google.a05_layoutdecoration_designforminformation, PID: 15798
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.google.a05_layoutdecoration_designforminformation/com.google.a05_layout_decoration_design_form_information.MainActivity: android.view.InflateException: Binary XML file line #9: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: android.view.InflateException: Binary XML file line #9: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.google.a05_layout_decoration_design_form_information.MainActivity.onCreate(MainActivity.java:12)
        at android.app.Activity.performCreate(Activity.java:6237)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:738)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161) 
        at com.google.a05_layout_decoration_design_form_information.MainActivity.onCreate(MainActivity.java:12) 
        at android.app.Activity.performCreate(Activity.java:6237) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5417) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-08 22:43:38.892 15798-15798/? I/Process: Sending signal. PID: 15798 SIG: 9

【问题讨论】:

能否请您发布form_info.xml的布局 请分享您的活动或有关初始组件的代码片段。 我添加了布局。 @AlanDeep 完整分享mainActivity 完成了。 @miladsalimi 【参考方案1】:

将您的 view component 更改为:

<View
    android:layout_
    android:layout_
    android:background="#000000"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"/>

使用View 而不是view

【讨论】:

以上是关于包含布局时如何解决 inflateException 问题?的主要内容,如果未能解决你的问题,请参考以下文章

自动布局:无法弄清楚如何解决与扩展 UITableViewCell 内的面板相关的警告

IE6 中 jQuery 对话框的修复或解决方法 - 对话框包含表单时的主要布局问题

Android:当布局已经包含按钮时,如何以编程方式覆盖整个布局?

在布局 xml 文件中使用包含时如何指定 id

自动布局:如何隐藏包含子视图的 UIView?

laravel:如何包含刀片布局中的部分?