gpt4 book ai didi

android - 通知开/关/选项单选按钮

转载 作者:行者123 更新时间:2023-11-30 03:14:16 26 4
gpt4 key购买 nike

我有一个菜单按钮,可以打开一个带有选项单选按钮的 Activity 。

我有 4 个选项,关闭通知,声音通知,振动通知和声音和振动通知。

编辑,我更改了我的代码,取得了一些进展,如果 if 情况为空,它工作正常。

代码如下:

public class NotificareOptions extends Activity {
RadioGroup radioGroup;
RadioButton radio1,radio2,radio3,radio4;
Button b1;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.menu_options);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radio1 = (RadioButton) findViewById(R.id.radioFara);
radio2 = (RadioButton) findViewById(R.id.radioSunet);
radio3 = (RadioButton) findViewById(R.id.radioVib);
radio4 = (RadioButton) findViewById(R.id.radioSunetVib);
//declare the onCheckChangedListener
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
//TODO Auto-generated method stub

if (checkedId == radio1.getId()) {
}
else
if (checkedId == radio2.getId()){
Notificare.notification.defaults = Notification.DEFAULT_SOUND;
Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
}
else
if (checkedId == radio3.getId()){
long[] vibration = {0,300,300,300};
Notificare.notification.vibrate = vibration;
Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
}
else
if (checkedId == radio4.getId()){
Notificare.notification.defaults = Notification.DEFAULT_SOUND;
long[] vibration = {0,300,300,300};
Notificare.notification.vibrate = vibration;
Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
}

}
});

radioGroup.check(radio2.getId());
}
}

我想向 Notification 类添加它需要的代码行,以便在它被触发时振动/发出声音等。

这是日志:

12-06 15:05:25.932: E/AndroidRuntime(1629): FATAL EXCEPTION: main
12-06 15:05:25.932: E/AndroidRuntime(1629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.NotificareOptions}: java.lang.NullPointerException
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.os.Looper.loop(Looper.java:137)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-06 15:05:25.932: E/AndroidRuntime(1629): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 15:05:25.932: E/AndroidRuntime(1629): at java.lang.reflect.Method.invoke(Method.java:511)
12-06 15:05:25.932: E/AndroidRuntime(1629): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-06 15:05:25.932: E/AndroidRuntime(1629): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-06 15:05:25.932: E/AndroidRuntime(1629): at dalvik.system.NativeStart.main(Native Method)
12-06 15:05:25.932: E/AndroidRuntime(1629): Caused by: java.lang.NullPointerException
12-06 15:05:25.932: E/AndroidRuntime(1629): at com.example.app.NotificareOptions$1.onCheckedChanged(NotificareOptions.java:45)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.widget.RadioGroup.access$600(RadioGroup.java:52)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.widget.CompoundButton.setChecked(CompoundButton.java:128)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.widget.RadioGroup.setCheckedStateForView(RadioGroup.java:179)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.widget.RadioGroup.check(RadioGroup.java:163)
12-06 15:05:25.932: E/AndroidRuntime(1629): at com.example.app.NotificareOptions.onCreate(NotificareOptions.java:65)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.Activity.performCreate(Activity.java:4465)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-06 15:05:25.932: E/AndroidRuntime(1629): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-06 15:05:25.932: E/AndroidRuntime(1629): ... 11 more

最佳答案

您通常应该将所有单选按钮包装在一个单选组中。在 RadioGroup 中,有以下回调可以为您提供所选项目。

http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

此外,我猜您正在使用 Notificare 库来触发通知。您正在创建它们,但您从不发出这些通知。您应该更仔细地查看库文档。

关于android - 通知开/关/选项单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425559/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com