I am a starter in Android Studio. My first app works just fine but when I select the home button it crashes.
Here is my HomeActivity.java:::
package com.example.ir_sensor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
public class HomeActivity extends AppCompatActivity {
private Switch homeAwaySwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
homeAwaySwitch = homeAwaySwitch.findViewById(R.id.Switch_HomeAway);
}
public void switchChange (View v) {
if (homeAwaySwitch.isChecked()) {
homeAwaySwitch.setText("Home");
} else if (!homeAwaySwitch.isChecked()) {
homeAwaySwitch.setText("Away");
}
}
}
Here is my activity_home.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=".HomeActivity">
<ImageView
android:id="@+id/Image_House"
android:layout_width="72dp"
android:layout_height="71dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:contentDescription="@string/image_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/house_menu" />
<TextView
android:id="@+id/textView"
android:layout_width="77dp"
android:layout_height="20dp"
android:layout_marginStart="24dp"
android:text="@string/location"
android:textAlignment="textStart"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/Image_House"
app:layout_constraintStart_toEndOf="@+id/Image_House"
app:layout_constraintTop_toTopOf="@+id/Image_House"
app:layout_constraintVertical_bias="1.0" />
<Switch
android:id="@+id/Switch_HomeAway"
android:layout_width="250dp"
android:layout_height="47dp"
android:layout_marginStart="80dp"
android:layout_marginTop="255dp"
android:onClick="switchChange"
android:text="Switch"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Image_House" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my LogCat:::
2019-08-18 19:42:54.636 15559-15559/com.example.ir_sensor E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ir_sensor, PID: 15559 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ir_sensor/com.example.ir_sensor.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.Switch.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 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:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.Switch.findViewById(int)' on a null object reference at com.example.ir_sensor.HomeActivity.onCreate(HomeActivity.java:17) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 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:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Can you help me?
Your homeAwaySwitch
is to be assigned to the Switch_HomeAway
ID of the context and not of the homeAwaySwitch
activity.
Try this
homeAwaySwitch = findViewById(R.id.Switch_HomeAway)