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
CodePudding user response:
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
CodePudding user response:
- 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);
...
}