我有一個按鈕變數并且一直在崩潰
這是 MainActivity.java:
package com.example.datasaving;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Button button = this.findViewById(R.id.SAVE);
TextView InputID = findViewById(R.id.textView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = InputID.getText().toString();
Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();
}
});
}
}
這是activity_main.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/SAVE"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/enter_your_gmail"
android:textColorHint="#757575"
app:layout_constraintBottom_toTopOf="@ id/SAVE"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
現在它給了我一個錯誤說:
FATAL EXCEPTION: main
Process: com.example.datasaving, PID: 6961
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datasaving/com.example.datasaving.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3921)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4078)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2423)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:334)
at android.app.ActivityThread.main(ActivityThread.java:8348)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.datasaving.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:8363)
at android.app.Activity.performCreate(Activity.java:8341)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4078)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2423)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:233)
at android.os.Looper.loop(Looper.java:334)
at android.app.ActivityThread.main(ActivityThread.java:8348)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1065)
所以這表示我有一個空物件,但是當我 ctrl 單擊該物件時,它會將我發送回按鈕可能有什么問題是我做錯了什么還是只是 android 作業室?
uj5u.com熱心網友回復:
取而代之的是,
Button button = this.findViewById(R.id.SAVE);
TextView InputID = findViewById(R.id.textView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
請這樣做。
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = this.findViewById(R.id.SAVE);
TextView InputID = findViewById(R.id.textView);
在您的代碼中,您正在設定R.id.textView并且R.id.SAVE當您的活動尚未通過 設定其布局setContentView時,因此當您呼叫
button.setOnClickListener{...}
它會崩潰,因為還沒有button參考,因為還沒有設定布局,因此NullPointerException
現在,請記住這 4 行并按順序排列。
- onCreate(Bundle savedInstanceState)
- super.onCreate(savedInstanceState)
- setContentView(R.layout.my_layout)
- findViewByid(R.id.my_view)
為了進一步幫助您,當您遇到可怕NullPointerException的或所謂的NPE時,不要驚慌,只需查看運算子的左側.并嘗試non-existent在呼叫它的一些時思考它的原因methods
uj5u.com熱心網友回復:
把你的初始化放在之后
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IE
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = this.findViewById(R.id.SAVE);
TextView InputID = findViewById(R.id.textView);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521261.html
標籤:爪哇安卓xml
上一篇:無法使用Python回圈遍歷XML(ElementTreeAPI)
下一篇:如何使2個視圖水平填充螢屏?
