1)我需要在警报触发时启动一个活动,我尝试在 BroadCastReceiver 的 onRecieve 事件中添加该活动,但它失败了。
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent){
Toast.makeText(context, "Recieved!!", Toast.LENGTH_LONG).show();
Activity act = new Activity();
act.startActivity(intent);
}
}
错误:
06-22 13:42:00.733: W/dalvikvm(750): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-22 13:42:00.763: E/AndroidRuntime(750): FATAL EXCEPTION: main
06-22 13:42:00.763: E/AndroidRuntime(750): java.lang.RuntimeException: Unable to start receiver com.exercise.AndroidTime.AlarmReceiver: java.lang.NullPointerException
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.access$2400(ActivityThread.java:117)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.os.Looper.loop(Looper.java:123)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-22 13:42:00.763: E/AndroidRuntime(750): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 13:42:00.763: E/AndroidRuntime(750): at java.lang.reflect.Method.invoke(Method.java:507)
06-22 13:42:00.763: E/AndroidRuntime(750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-22 13:42:00.763: E/AndroidRuntime(750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-22 13:42:00.763: E/AndroidRuntime(750): at dalvik.system.NativeStart.main(Native Method)
06-22 13:42:00.763: E/AndroidRuntime(750): Caused by: java.lang.NullPointerException
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.Activity.startActivityForResult(Activity.java:2827)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.Activity.startActivity(Activity.java:2933)
06-22 13:42:00.763: E/AndroidRuntime(750): at com.exercise.AndroidTime.AlarmReceiver.onReceive(AlarmReceiver.java:23)
06-22 13:42:00.763: E/AndroidRuntime(750): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)
06-22 13:42:00.763: E/AndroidRuntime(750): ... 10 more
2)即使按下后退按钮,我如何使活动继续运行。当满足源代码中的某些条件时,活动将关闭。例如警报不会响起,他也无法关闭应用程序,直到用户正确回答问题。