android闹钟应用程序崩溃

时间:2017-05-23 09:15:43

标签: android alarmmanager android-alarms

我正在尝试使用android studio制作简单的闹钟应用程序...我      认为我的代码是正确的,虽然它崩溃了。我认为问题在于       报警接收器和服务类....我使用if-else指定是否报警     打开,铃声关闭或闹钟开启或铃声关闭等等....     请帮忙....谢谢

这是代码:

会这样做:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.lakshya.wakeywakey, PID: 2120
                  java.lang.RuntimeException: Unable to start service com.example.lakshya.wakeywakey.RingtonePlayService@4a8166c0 with Intent { cmp=com.example.lakshya.wakeywakey/.RingtonePlayService (has extras) }: java.lang.NullPointerException: println needs a message
                      at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2717)
                      at android.app.ActivityThread.access$2100(ActivityThread.java:135)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:136)
                      at android.app.ActivityThread.main(ActivityThread.java:5019)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: java.lang.NullPointerException: println needs a message
                      at android.util.Log.println_native(Native Method)
                      at android.util.Log.e(Log.java:232)
                      at com.example.lakshya.wakeywakey.RingtonePlayService.onStartCommand(RingtonePlayService.java:37)
                      at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2700)
                      at android.app.ActivityThread.access$2100(ActivityThread.java:135) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:136) 
                      at android.app.ActivityThread.main(ActivityThread.java:5019) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                      at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

您的错误在onStartCommand方法

我想在那一行,因为bundle可以为null或者不包含字符串" extra"

String state = intent.getExtras().getString("extra");
Log.e("Ringtone extra is" ,state);

只需添加check for null

String state = intent.getExtras().getString("extra");
if (state == null)
   state = "null";
Log.e("Ringtone extra is" ,state);