在Switch上设置onSetCheckedListener时获取运行时异常

时间:2016-12-17 15:27:39

标签: android nullpointerexception runtimeexception

我得到了这个例外 -
java.lang.RuntimeException:无法启动活动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.

请告诉我哪里出错了。 在此先感谢:)

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

你的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);