英文:
how to solve this error : 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
问题
XML 代码和 Java 代码中的注释将不被翻译,以下是翻译好的内容:
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"
android:background="@drawable/wall_grad"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="The counter app"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:text="0"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_heading" />
<Button
android:id="@+id/button_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_counter"
app:layout_constraintVertical_bias="0.198" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java 代码:
package com.example.counter_08_01_23;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView heading1, counter_text1;
Button btn1;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
heading1 = findViewById(R.id.textView_heading);
counter_text1 = findViewById(R.id.textView_counter);
btn1 = findViewById(R.id.button_counter);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 添加功能
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter_text1.setText(inc_counter());
}
});
}
public int inc_counter() {
return ++counter;
}
}
日志信息:
> E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.counter_08_01_23, PID: 22639
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.counter_08_01_23/com.example.counter_08_01_23.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:3197)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3334)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7191)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
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.counter_08_01_23.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:7376)
at android.app.Activity.performCreate(Activity.java:7367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3177)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3334)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7191)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
I/Process: Sending signal. PID: 22639 SIG: 9
请注意,上述日志中的错误信息指出按钮对象为空,因此需要确保在 setContentView(R.layout.activity_main);
之后初始化按钮对象。
英文:
I am trying to code a counter app in which pressing the "Button" increments the counter and displays it in the TextView. I followed the exact same procedure as my online teacher. But, the code is not running.
XML code:
<?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"
android:background="@drawable/wall_grad"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="The counter app"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:text="0"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_heading" />
<Button
android:id="@+id/button_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView_counter"
app:layout_constraintVertical_bias="0.198" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java Code:
package com.example.counter_08_01_23;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView heading1 , counter_text1;
Button btn1;
int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
heading1 = findViewById(R.id.textView_heading);
counter_text1 = findViewById(R.id.textView_counter);
btn1 = findViewById(R.id.button_counter);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Adding functionality
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
counter_text1.setText(inc_counter());
}
});
}
public int inc_counter(){
return ++counter;
}
}
The Log is as follows :
>
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.counter_08_01_23, PID: 22639
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.counter_08_01_23/com.example.counter_08_01_23.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:3197)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3334)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7191)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
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.counter_08_01_23.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:7376)
at android.app.Activity.performCreate(Activity.java:7367)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3177)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3334)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2025)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7191)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)
I/Process: Sending signal. PID: 22639 SIG: 9
答案1
得分: 2
初始化视图并设置内容
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heading1 = findViewById(R.id.textView_heading);
counter_text1 = findViewById(R.id.textView_counter);
btn1 = findViewById(R.id.button_counter);
您可以使用视图绑定来使您的工作更加轻松
英文:
Initialise views below set content
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heading1 = findViewById(R.id.textView_heading);
counter_text1 = findViewById(R.id.textView_counter);
btn1 = findViewById(R.id.button_counter);
You can use view binding which make your work more easier
答案2
得分: 0
- 在这里,你只需要将上下的行交换一下顺序。因为你首先声明了findViewById()
,然后才充气XML布局。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 提升你的布局到Java类级别。
setContentView(R.layout.activity_main);
// 然后从XML中查找你的视图
btn1 = findViewById(R.id.button_counter);
...
}
英文:
- Here, you need to change just the line up and down. Because you declare findViewById()
first of inflating XML Layout.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Raise your layout to Java class level.
// Then find Your view from xml
btn1 = findViewById(R.id.button_counter);
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论