Android 在交换机上设置onSetCheckedListener时获取运行时异常

Android 在交换机上设置onSetCheckedListener时获取运行时异常,android,nullpointerexception,runtimeexception,Android,Nullpointerexception,Runtimeexception,我得到了这个例外- java.lang.RuntimeException:无法启动活动组件信息:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)” 这是我的主要活动代码- package com.example.toushif.myapp

我得到了这个例外-
java.lang.RuntimeException:无法启动活动组件信息:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Switch.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)”

这是我的主要活动代码-

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"/>
请告诉我哪里出错了。
提前感谢:)

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

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

当试图调用
sw2

上的方法时,如果oncreate()方法中有错误,则会出现NPE

你有这个:

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);
sw2 = (Switch)findViewById(R.id.switchh2);