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

时间:2013-12-06 13:49:51

标签: android notifications radio-button options

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

我有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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

我保存了一个带有共享偏好的变量,我用它来实现我想要的通知服务类。

        if (checkedId == radio1.getId()) {
             Editor e1 = prefs.edit();
             e1.putString(OptionsPos, "1");
             e1.commit();
             } 
        else 
            if (checkedId == radio2.getId()){   
            //  Notificare.notification.defaults = Notification.DEFAULT_SOUND;
            //  Notificare.notificationManager.notify(Notificare.uniqueID, Notificare.notification);
                 Editor e1 = prefs.edit();
                 e1.putString(OptionsPos, "2");
                 e1.commit();
             }
        else 
....