简体   繁体   中英

I encountered this error in android studio while learning it

I encountered this error in android studio while learning it:

08-20 11:56:52.192 9815-9815/com.example.android.courtcountee1 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.courtcountee1, PID: 9815 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.courtcountee1/com.example.android.courtcountee1.MainActivity}: android.view.InflateException: Binary XML file line #66: 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:3319) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.access$1100(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.inter nal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: android.view.InflateException: Binary XML file line #66: 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:551) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.example.android.courtcountee1.MainActivity.onCreate(MainActivity.java:15) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266) at android.app.Ac tivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.access$1100(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 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:750) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716) at android.view.LayoutInflater.rInflate(LayoutInflater.java:847) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.ja va:810) at android.view.LayoutInflater.rInflate(LayoutInflater.java:855) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810) at android.view.LayoutInflater.inflate(LayoutInflater.java:527) at android.view.LayoutInflater.inflate(LayoutInflater.java:429) at android.view.LayoutInflater.inflate(LayoutInflater.java:380) at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) at com.example.android.courtcountee1.MainActivity.onCreate(MainActivity.java:15) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.access$1100(ActivityThread.java:229) at android.app.ActivityThr ead$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

The XML

i THINK THE VIEW TAG IS CAUSING THE PROBLEM AS REMOVING IT RESOLVED THIS

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">


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

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="24dp"
            android:layout_weight="1"
            android:orientation="vertical"
            tools:context="com.example.android.courtcounter.MainActivity">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="Team A" />

            <TextView
                android:id="@+id/team_a_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:fontFamily="sans-serif-light"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="0"
                android:textColor="#000000"
                android:textSize="56dp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="threePoints"
                android:text="+3 POINTS" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="twoPoints"
                android:text="+2 POINTS" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="freeThrow"
                android:text="Free Throw" />
        </LinearLayout>

        <!--<view
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="24dp"
            android:layout_marginRight="24dp"
            android:background="@android:color/darker_gray">
        </view>-->

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:orientation="vertical"
            tools:context=".MainActivity">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="Team B"
                android:textColor="#616161"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/team_b_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:fontFamily="sans-serif-light"
                android:gravity="center_horizontal"
                android:padding="4dp"
                android:text="0"
                android:textColor="#000000"
                android:textSize="56dp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="threePointsB"
                android:text="+3 POINTS" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="twoPointsB"
                android:text="+2 POINTS" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="freeThrowB"
                android:text="Free Throw" />
        </LinearLayout>

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="32dp"
        android:onClick="reset"
        android:text="RESET" />
</RelativeLayout>

View tag must start with the capital letter, like this:

<View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:background="@android:color/darker_gray">
</View>

In fact,Every xml View tag will through the LayoutInflater class construct a real View class. About LayoutInflater , It will combine the xml tag with the View class in package android.view. android.widgetandroid.webkitandroid.app . About you use the view tag, you should use like this.

    <view
    android:id="@+id/test"
    android:layout_width="40dp"
    android:layout_height="40dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    class="androidx.constraintlayout.utils.widget.ImageFilterView"
    android:background="@color/colorAccent"
    />

you must appoint the target class. because the LayoutInflater tell us should do.

    View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
        boolean ignoreThemeAttr) {
    if (name.equals("view")) {
        name = attrs.getAttributeValue(null, "class");
    }

    // Apply a theme wrapper, if allowed and one is specified.
    if (!ignoreThemeAttr) {
        final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
        final int themeResId = ta.getResourceId(0, 0);
        if (themeResId != 0) {
            context = new ContextThemeWrapper(context, themeResId);
        }
        ta.recycle();
    }

    if (name.equals(TAG_1995)) {
        // Let's party like it's 1995!
        return new BlinkLayout(context, attrs);
    }
    ...

because you not appoint the class.It can not find the real class about it,So It cause the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM