使用Dialog Alert创建警报

时间:2013-11-22 17:12:20

标签: android android-alertdialog

单击AlertDialog中的按钮是否可以设置闹钟?我想在点击AlertDialog的贪睡按钮时创建一个闹钟。

builder.setNegativeButton("Snooze",  new DialogInterface.OnClickListener()  //@Override


        public void onClick(DialogInterface dialog, int which)  {

            Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");


            PendingIntent operation = PendingIntent.getActivity(getActivity().getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
            AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
            long time= System.currentTimeMillis();
            EditText text=(EditText)getActivity().findViewById(R.id.editText1);
            String str = text.getText().toString();
            long t=Long.parseLong(str);
             alarmManager.set(AlarmManager.RTC_WAKEUP,
                     time+( t*60*1000), operation);


        }
    });

ERROR:

11-22 19:52:23.018: E/AndroidRuntime(363): FATAL EXCEPTION: main
11-22 19:52:23.018: E/AndroidRuntime(363): java.lang.NullPointerException
11-22 19:52:23.018: E/AndroidRuntime(363):  at com.example.servicealarmdemo2.AlertDemo$2.onClick(AlertDemo.java:60)
11-22 19:52:23.018: E/AndroidRuntime(363):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
11-22 19:52:23.018: E/AndroidRuntime(363):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 19:52:23.018: E/AndroidRuntime(363):  at android.os.Looper.loop(Looper.java:123)
11-22 19:52:23.018: E/AndroidRuntime(363):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 19:52:23.018: E/AndroidRuntime(363):  at java.lang.reflect.Method.invokeNative(Native Method)
11-22 19:52:23.018: E/AndroidRuntime(363):  at java.lang.reflect.Method.invoke(Method.java:507)
11-22 19:52:23.018: E/AndroidRuntime(363):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 19:52:23.018: E/AndroidRuntime(363):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 19:52:23.018: E/AndroidRuntime(363):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

看看这个tutorial。通过它,尝试解决它,花很多时间在它上面。当你把自己逼到极限时,你会学得最好。然后当有人给出好的回答时,点击答案旁边的复选标记接受答案。祝你好运,欢迎来到Android!

答案 1 :(得分:0)

getActivity().getBaseContext()替换为getActivity()