在 Switch 上设置 onSetCheckedListener 时出现运行时异常

Getting a Runtime Exception when set onSetCheckedListener on Switch

我遇到了这个异常-
java.lang.RuntimeException:无法启动 activity ComponentInfo:java.lang.NullPointerException:尝试在空对象引用

上调用虚拟方法 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)'

这是我的 MainActivity 代码-

package com.example.toushif.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends Activity implements 
CompoundButton.OnCheckedChangeListener {

CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
Switch sw1;
Switch sw2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    cb1 = (CheckBox)findViewById(R.id.cb1);
    cb2 = (CheckBox)findViewById(R.id.cb2);
    cb3 = (CheckBox)findViewById(R.id.cb3);
    sw1 = (Switch)findViewById(R.id.switchh1);
    sw1 = (Switch)findViewById(R.id.switchh2);
    sw1.setOnCheckedChangeListener(this);
    sw2.setOnCheckedChangeListener(this);
    cb1.setOnCheckedChangeListener(this);
    cb2.setOnCheckedChangeListener(this);
    cb3.setOnCheckedChangeListener(this);

}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    if(cb1.isChecked())
    {
        cb1.setText("Check Box 1 is checked");
    }
    else
    {
        cb1.setText("Check Box 1");
    }
    if(cb2.isChecked())
    {
        cb2.setText("Check Box 2 is checked");
    }
    else
    {
        cb2.setText("Check Box 2");
    }
    if(cb3.isChecked())
    {
        cb3.setText("Check Box 3 is checked");
    }
    else
    {
        cb3.setText("Check Box 3");
    }
    if(sw1.isChecked())
    {
        sw1.setText("Switch 1 ON");
    }
    else
    {
        sw1.setText("Switch 1 is OFF");
    }
    if(sw2.isChecked())
    {
        sw2.setText("Switch 2 ON");
    }
    else
    {
        sw2.setText("Switch 2 is OFF");
    }
}

}

这是我的布局-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 1"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 2"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Radio Button 3"/>
    </RadioGroup>

</LinearLayout>

<LinearLayout
    android:id="@+id/linear2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_toRightOf="@id/linear1"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    >
    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check box 1"/>
    <CheckBox
        android:id="@+id/cb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check box 2"/>
    <CheckBox
        android:id="@+id/cb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check box 3"/>

</LinearLayout>

<LinearLayout
    android:id="@+id/linear3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/linear1"
    android:orientation="horizontal"
    >
    <Switch
        android:id="@+id/switchh1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch 1"/>
    <Switch
        android:id="@+id/switchh2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:text="Switch 2"/>

</LinearLayout>

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is simple text view."
    android:textSize="18dp"
    android:textStyle="bold"
    android:layout_below="@id/linear3"/>
<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/text1"
    android:textSize="18dp"
    android:textStyle="italic"/>
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/koala"
    android:layout_alignParentBottom="true"/>

我在 运行-

时遇到以下异常
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.toushif.myapplication, PID: 4129
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toushif.myapplication/com.example.toushif.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
                  at com.example.toushif.myapplication.MainActivity.onCreate(MainActivity.java:27)
                  at android.app.Activity.performCreate(Activity.java:6679)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Application terminated.

请告诉我哪里出错了。 提前致谢:)

您忘记初始化 sw2 但初始化了 sw1 两次:

sw1 = (Switch)findViewById(R.id.switchh1);
sw1 = (Switch)findViewById(R.id.switchh2);

尝试调用 sw2 上的方法时出现 NPE。

您的 oncreate() 方法有误。

你有这个:

cb1 = (CheckBox)findViewById(R.id.cb1);
cb2 = (CheckBox)findViewById(R.id.cb2);
cb3 = (CheckBox)findViewById(R.id.cb3);
sw1 = (Switch)findViewById(R.id.switchh1);
sw1 = (Switch)findViewById(R.id.switchh2);
sw1.setOnCheckedChangeListener(this);
sw2.setOnCheckedChangeListener(this);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);

你的变量是:

CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
Switch sw1;
Switch sw2;

所以 sw2 是空的,因为:

sw1 = (Switch)findViewById(R.id.switchh2);

尝试更改为:

sw2 = (Switch)findViewById(R.id.switchh2);