>我添加了一个睡眠计时器,以便在用户输入的指定时间后关闭应用程序,一切正常,但是当时间结束时,应用程序崩溃而不仅仅是关闭。 我有一个睡眠定时器类,我添加了关闭应用程序的代码,如下所示。它是系统退出(0);是否有另一件事可以避免应用程序崩溃。?
private void startTimer() {
mEndTime = System.currentTimeMillis() + mTimeLeftInMillis;
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
@Override
public void onFinish() {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}.start();
mTimerRunning = true;
updateWatchInterface();
}
当计时器完成倒计时时,应用程序崩溃而不是关闭,我在安卓工作室 Logcat 中看到以下错误。
**com.elmondal.radiomisr:serviceProcess E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.elmondal.radiomisr:serviceProcess, PID: 22920
java.lang.RuntimeException: Unable to start service com.elmondal.radiomisr.service.PlayerService@2112053 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3686)
at android.app.ActivityThread.access$1600(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.elmondal.radiomisr.service.PlayerService.onStartCommand(**PlayerService.java**:62)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3667)
at android.app.ActivityThread.access$1600(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)**
在上面的错误中,播放器服务器中有一个 onStart命令的链接。JAVA,当我单击它时,它会在播放器服务中显示下面的代码。.JAVA。
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
streamUrl = intent.getStringExtra(AppConstants.STREAM_URL);
channelName = intent.getStringExtra(AppConstants.CHANNEL_NAME);
startPlayer(channelName, streamUrl);
pushServiceToForeground();
initializePhoneStateListener();
playerService = this;
return START_STICKY;
}
java.lang.RuntimeException: Unable to start service com.elmondal.radiomisr.service.PlayerService@2112053 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
错误非常明确。在 onStartCommand 中,intent
为 null。因此,您不能调用getStringExtra
。
此外,您应该考虑正确终止您的活动/服务,而不是试图终止进程。