因此我将单选按钮turnOn和turnOff组合在一起,并将响亮,振动和静音分组为RadioGroup。它们是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();
}
当我评论出无线电按钮切换方法时,它不会崩溃。
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>