在一个简单的应用程序中获取java.lang.NullPointerException:

我正在获取java.lang.NullPointerException。而且我不知道如何解决。在这里寻求帮助 我试过的

to look here java.lang.RuntimeException: Unable to start activity ComponentInfo But, it didn't help me

清理并重建项目。 在Android Studio中使缓存无效并重新启动

and i'v discovered some problems in LayoutInflater.java It cant resolve some imports" sorry cant import images yet this is and image of errors in LayoutInflater.java

我的XML文件activity_main.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="Team A" />

            <TextView
                android:id="@+id/team_a_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="24dp"
                android:gravity="center"
                android:textSize="50sp"
                android:text="0" />

            <Button
                android:id="@+id/three_points_button_a"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:onClick="plusThreePointsA"
                android:text="+3 Points" />

            <Button
                android:id="@+id/two_points_button_a"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:onClick="plusTwoPointsA"
                android:text="+2 Points" />

            <Button
                android:id="@+id/free_throw_button_a"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:onClick="plusOnePointA"
                android:text="Free Throw" />
        </LinearLayout>
        <view
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:orientation="vertical">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="16dp"
                android:gravity="center"
                android:text="Team B" />

            <TextView
                android:id="@+id/team_b_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="24dp"
                android:gravity="center"
                android:textSize="50sp"
                android:text="0" />

            <Button
                android:id="@+id/three_points_button_b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:onClick="plusThreePointsB"
                android:text="+3 Points" />

            <Button
                android:id="@+id/two_points_button_b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:onClick="plusTwoPointsB"
                android:text="+2 Points" />

            <Button
                android:id="@+id/free_throw_button_b"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:onClick="plusOnePointB"
                android:text="Free Throw" />


        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom|center"
        android:orientation="vertical">

        <Button
            android:id="@+id/reset_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="24dp"
            android:onClick="reset"
            android:text="Reset" />
    </LinearLayout>


</LinearLayout>

我的MainActivity.java

package com.example.problems;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    int scoreA = 0;
    int scoreB = 0;

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

    public void plusThreePointsA(View view){
        scoreA = scoreA+3;
        displayScoreA(scoreA);
    }

    public void plusTwoPointsA(View view){
        scoreA = scoreA+2;
        displayScoreA(scoreA);
    }

    public void plusOnePointA(View view){
        scoreA = scoreA+1;
        displayScoreA(scoreA);
    }

    public void plusThreePointsB(View view){
        scoreB = scoreB+3;
        displayScoreB(scoreB);
    }

    public void plusTwoPointsB(View view){
        scoreB = scoreB+2;
        displayScoreB(scoreB);
    }

    public void plusOnePointB(View view){
        scoreB = scoreB+1;
        displayScoreB(scoreB);
    }

    public void displayScoreA (int score){
        TextView viewScore = findViewById(R.id.team_a_score);
        viewScore.setText(String.valueOf(score));

    }
    public void displayScoreB (int score){
        TextView viewScore = findViewById(R.id.team_b_score);
        viewScore.setText(String.valueOf(score));

    }

    public void reset (View view){
        scoreA = 0;
        scoreB = 0;

        displayScoreA(scoreA);
        displayScoreB(scoreB);

    }
}

我得到的错误

    --------- beginning of crash
05-11 08:14:25.720 4084-4084/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.problems, PID: 4084
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.problems/com.example.problems.MainActivity}: java.lang.NullPointerException: 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:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     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:715)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
        at com.example.problems.MainActivity.onCreate(MainActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
        at android.app.ActivityThread.access$800(ActivityThread.java:144) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5221) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
05-11 08:19:26.000 4084-4084/com.example.problems I/Process: Sending signal. PID: 4084 SIG: 9
评论