Android Studio中的“java.lang.NullPointerException”,我正在尝试从编辑文本视图获取文本,但遇到此错误

vwoqyblh  于 2022-10-15  发布在  Android
关注(0)|答案(1)|浏览(203)
public class Login extends AppCompatActivity {

    BazaDeDate bazaDeDate;
    EditText etIdUtilizator;
    EditText etParola;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        bazaDeDate = new BazaDeDate(this);
        etIdUtilizator = findViewById(R.id.etIdUtilizator);
        etParola = findViewById(R.id.etParolaUtilizator);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void activitateUrmatoare(View view) {
        Toast.makeText(this, etIdUtilizator.getText(), Toast.LENGTH_LONG).show();
    }
}

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.consilierepsihologicafirma, PID: 6462
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446)
        at android.view.View.performClick(View.java:5198)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        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.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441)
        at android.view.View.performClick(View.java:5198) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        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 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.example.consilierepsihologicafirma.activitati.Login.activitateUrmatoare(Login.java:37)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) 
        at android.view.View.performClick(View.java:5198) 
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        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) 
5fjcxozz

5fjcxozz1#

必须在setContent view之后用id初始化:

public class Login extends AppCompatActivity {

    BazaDeDate bazaDeDate;
    EditText etIdUtilizator;
    EditText etParola;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bazaDeDate = new BazaDeDate(this);
        etIdUtilizator = findViewById(R.id.etIdUtilizator);
        etParola = findViewById(R.id.etParolaUtilizator);

    }

    public void activitateUrmatoare(View view) {
        Toast.makeText(this, etIdUtilizator.getText(), Toast.LENGTH_LONG).show();
    }
}

相关问题