Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取单选按钮切换方法上的空指针异常_Java_Android - Fatal编程技术网

Java 获取单选按钮切换方法上的空指针异常

Java 获取单选按钮切换方法上的空指针异常,java,android,Java,Android,所以我把单选按钮“打开”和“关闭”组合在一起,把“响亮”、“振动”和“无声”组合成一个组。它们是setContentView之后的initailozid turnOn = (RadioButton) findViewById(R.id.on); turnOff = (RadioButton) findViewById(R.id.off); loud = (RadioButton) findViewById(R.id.sound); vibrate = (Radio

所以我把单选按钮“打开”和“关闭”组合在一起,把“响亮”、“振动”和“无声”组合成一个组。它们是setContentView之后的initailozid

    turnOn = (RadioButton) findViewById(R.id.on);
    turnOff = (RadioButton) findViewById(R.id.off);
    loud = (RadioButton) findViewById(R.id.sound);
    vibrate = (RadioButton) findViewById(R.id.vibrate);
    silent = (RadioButton) findViewById(R.id.Silent);
当我尝试检查单选按钮时出现异常

    if(s.onoff){
        turnOn.setChecked(true);
    }
    else{
        turnOff.setChecked(true);
    }  
    Log.d("update ui", "profile"); 
        if(s.profile.equals("0")){
        loud.toggle();
        }
    else if(s.profile.equals("1")){
        vibrate.toggle();
    }
    else{
        silent.toggle();   
    }
当我注释掉radiobuttons切换方法时,它没有崩溃

07-14 17:32:55.350: E/AndroidRuntime(8120): FATAL EXCEPTION: main
07-14 17:32:55.350: E/AndroidRuntime(8120): java.lang.RuntimeException: Unable to start activity ComponentInfo{hasebou.karim.simplify/hasebou.karim.simplify.Volume}: java.lang.NullPointerException
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.os.Looper.loop(Looper.java:137)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.ActivityThread.main(ActivityThread.java:4898)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at java.lang.reflect.Method.invokeNative(Native Method)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at java.lang.reflect.Method.invoke(Method.java:511)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at dalvik.system.NativeStart.main(Native Method)
07-14 17:32:55.350: E/AndroidRuntime(8120): Caused by: java.lang.NullPointerException
07-14 17:32:55.350: E/AndroidRuntime(8120):     at hasebou.karim.simplify.Volume.updateUi(Volume.java:78)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at hasebou.karim.simplify.Volume.onCreate(Volume.java:43)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.Activity.performCreate(Activity.java:5206)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-14 17:32:55.350: E/AndroidRuntime(8120):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-14 17:32:55.350: E/AndroidRuntime(8120):     ... 11 more
这是我的xml文件

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Volume"
    android:orientation="vertical"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#FFFFFF"
     >

    <TimePicker
        android:id="@+id/ChoosenTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center" >

        <RadioButton
            android:id="@+id/on"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Turn on"
            android:onClick="onoff"
             />

        <RadioButton
            android:id="@+id/off"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onoff"
            android:text="Turn off" />

    </RadioGroup>
</LinearLayout>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Action schedule"
        android:textSize="18sp"/>
    <View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:background="#31B6E7"/>
    <HorizontalScrollView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        >
       <ToggleButton
        android:id="@+id/sun"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Sun"
        android:textOff="Sun"
        android:onClick="daysOfWeek"
        android:background="@drawable/togglebutton"
         />
       <ToggleButton
        android:id="@+id/mon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Mon"
        android:textOff="Mon"
        android:onClick="daysOfWeek"
        android:background="@drawable/togglebutton"
         />
       <ToggleButton
        android:id="@+id/tue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Tue"
        android:textOff="Tue"
        android:onClick="daysOfWeek"
        android:background="@drawable/togglebutton"
         />
       <ToggleButton
        android:id="@+id/wed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Wed"
        android:textOff="Wed"
        android:onClick="daysOfWeek"
        android:background="@drawable/togglebutton"
         />
       <ToggleButton
        android:id="@+id/thu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Thu"
        android:textOff="Thu"
        android:onClick="daysOfWeek"
        android:background="@drawable/togglebutton"
         />
       <ToggleButton
        android:id="@+id/fri"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Fri"
        android:textOff="Fri"
        android:onClick="daysOfWeek"
        android:background="@drawable/togglebutton" />
       <ToggleButton
        android:id="@+id/sat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Sat"
        android:textOff="Sat"
        android:onClick="daysOfWeek"
        android:background="@drawable/togglebutton"
         />

    </LinearLayout>
    </HorizontalScrollView >
<View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:background="#31B6E7"/>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <CheckBox
        android:id="@+id/wifi"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Wi-Fi"
        android:onClick="checkBoxClicked" />

    <CheckBox
        android:id="@+id/internet"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:onClick="checkBoxClicked"
        android:text="Mobile data" />

</LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
      <CheckBox
        android:id="@+id/gps"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="GPS"
        android:onClick="checkBoxClicked" />
    <CheckBox
        android:id="@+id/bluetooth"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Bluetooth"
        android:onClick="checkBoxClicked" />
</LinearLayout>
    <CheckBox
        android:id="@+id/airplaneMode"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Airplane mode"
        android:onClick="checkBoxClicked" />
        <View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:background="#31B6E7"/>
        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Phone profile"
        android:textSize="18sp"/>
     <RadioGroup
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/sound"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:onClick="onoff"
            android:layout_weight="1"
            android:text="Sound" />
        <RadioButton
            android:id="@+id/vibrate"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:onClick="onoff"
            android:layout_weight="1"
            android:text="Vibrate" />
        <RadioButton
            android:id="@+id/Silent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Silent"
            android:layout_weight="1"
            android:onClick="onoff"
             />
    </RadioGroup>

     <EditText
         android:id="@+id/EventName"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ems="10"
         android:text="Event name" >
     </EditText>

</LinearLayout>

你检查过那些RadioButton变量都不为空吗?请发布你的xml布局你要么把布局弄混了,要么把ID弄混了。至少有一个单选按钮为空。布局中是否有设置为“活动”的单选按钮哪一行是卷的第78行?