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

How to resolve problem of inflateException when I include a layout?

我设计了一个新的表单名称 (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_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

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

    <CheckBox
        android:id="@+id/cb_share_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Share my phone number"
        android:checked="true"/>

    <view
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#000000"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="50dp"
            android:layout_height="49dp"
            android:layout_weight="0"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/email_icon" />

        <EditText
            android:id="@+id/input_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Email"
            android:inputType="textEmailAddress" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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>
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>
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 ComponentInfo{com.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

将您的 view component 更改为:

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"/>

使用 View 代替 view