如何修复Error RuntimeException:无法在空对象引用上启动服务

时间:2019-05-28 23:52:39

标签: android null sleep countdowntimer runtimeexception

我添加了一个睡眠计时器,以在用户输入指定时间后关闭应用程序,一切正常,但是当时间结束时,应用程序崩溃而不仅仅是关闭。 我有一个用于睡眠计时器的类,并且添加了关闭该应用程序的代码,如下所示。

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();
    }

当计时器完成倒数计时时,应用程序崩溃而不是关闭,而我在android studio 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)** 

在上述错误中,playerservise.JAVA中有一个onStart命令的链接,当我单击它时,它向我显示了playerservise.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;
    }

1 个答案:

答案 0 :(得分:0)

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为空。因此,您无法在其上调用getStringExtra

此外,您应该考虑适当地终止活动/服务,而不是试图终止该进程。