为什么我的应用在第二次活动时崩溃?

时间:2019-08-18 16:56:31

标签: android crash

我是Android Studio的入门者。 我的第一个应用程序运行正常,但是当我选择“主页”按钮时,它崩溃了

这是我的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");
    }
}
}

这是我的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>

这是我的LogCat :::

  

2019-08-18 19:42:54.636 15559-15559 / com.example.ir_sensor E / AndroidRuntime:FATAL EXCEPTION:main       流程:com.example.ir_sensor,PID:15559       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.ir_sensor / com.example.ir_sensor.HomeActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android.widget.Switch。空对象引用上的findViewById(int)'           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016)           在android.os.Handler.dispatchMessage(Handler.java:107)           在android.os.Looper.loop(Looper.java:214)           在android.app.ActivityThread.main(ActivityThread.java:7356)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)        原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.widget.Switch.findViewById(int)”           在com.example.ir_sensor.HomeActivity.onCreate(HomeActivity.java:17)           在android.app.Activity.performCreate(Activity.java:7802)           在android.app.Activity.performCreate(Activity.java:7791)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016)           在android.os.Handler.dispatchMessage(Handler.java:107)           在android.os.Looper.loop(Looper.java:214)           在android.app.ActivityThread.main(ActivityThread.java:7356)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

您的homeAwaySwitch将分配给上下文的Switch_HomeAway ID,而不是homeAwaySwitch活动的ID。

尝试一下

homeAwaySwitch = findViewById(R.id.Switch_HomeAway)