在 Edittext 上获取空对象引用

Getting Null Object reference on Edittext

我知道这个问题看起来与 SO 上的其他问题相似,但我已经尝试了其他类似问题中的所有建议,但没有成功。

我收到这个错误:

2020-08-03 20:41:21.372 8916-8916/com.example.spacing E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.spacing, PID: 8916
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spacing/com.example.spacing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:159)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
        at android.content.Context.obtainStyledAttributes(Context.java:675)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
        at com.example.spacing.MainActivity.<init>(MainActivity.java:15)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2020-08-03 20:41:21.411 8916-8916/com.example.spacing I/Process: Sending signal. PID: 8916 SIG: 9

关于此代码:

    package com.example.spacing;
    
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    
    
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
        public int firstValue;
        EditText first;
        String firstString;
    
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            first = (EditText)findViewById(R.id.firstNum);
            firstString = first.getText().toString();
    
            Button Go = (Button) findViewById(R.id.goButton);
            Go.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                        firstValue = Integer.parseInt(firstString);
                        System.out.print(firstValue);
                }

        });
    }


}

这里是 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">


    <Button
        android:id="@+id/goButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/buttonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.898" />

    <TextView
        android:id="@+id/FirstAttribute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/spacedMaterial"
        app:layout_constraintBottom_toTopOf="@+id/goButton"
        app:layout_constraintEnd_toStartOf="@+id/firstNum"
        app:layout_constraintHorizontal_bias="0.457"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.043" />

    <EditText
        android:id="@+id/firstNum"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:ems="10"
        android:hint="@string/inMM"
        android:importantForAutofill="no"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/FirstAttribute"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.934"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/FirstAttribute"
        app:layout_constraintVertical_bias="0.476" />



    <TextView
        android:id="@+id/secondAttribute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/amount"
        app:layout_constraintBottom_toTopOf="@+id/goButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.073"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.131" />

    <EditText
        android:id="@+id/secondNumber"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:ems="10"
        android:hint="@string/number"
        android:importantForAutofill="no"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/secondAttribute"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.934"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/secondAttribute" />

    <TextView
        android:id="@+id/thirdAttribute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/areaWidth"
        app:layout_constraintBottom_toTopOf="@+id/goButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.068"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.218" />

    <EditText
        android:id="@+id/thirdNumber"
        android:layout_height="40dp"
        android:layout_width="165dp"
      android:ems="10"
        android:hint="@string/inMM"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="@+id/thirdAttribute"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.934"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/thirdAttribute"
        app:layout_constraintVertical_bias="0.476"
        android:importantForAutofill="no" />

</androidx.constraintlayout.widget.ConstraintLayout>

我尝试过的建议:

我尝试将编辑文本的初始化更改为创建之前和之后。 我已确保所有 ID 都正确并且引用了正确的内容。

还有大约 5 个我记不起来的东西,但 none 在我尝试构建时其中的一些东西起作用了。

感谢任何帮助。

问题是: 您正在尝试初始化单击侦听器外部的“firstString”变量,并在创建 Activity 后立即初始化,这意味着该变量将始终为空对象,因为您实际上并未在 EditText 中写入内容。

只需在ClickListener中初始化String即可!

` 包 com.example.spacing;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;


import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    public int firstValue;
    EditText first;
    String firstString;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        first = (EditText)findViewById(R.id.firstNum);
        

        Button Go = (Button) findViewById(R.id.goButton);
        Go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    firstString = first.getText().toString();
                    firstValue = Integer.parseInt(firstString);
                    System.out.print(firstValue);
            }

    });
}

}`