首页 > 解决方案 > how to fix Error RuntimeException: Unable to start service on a null object reference when use System.exit(0);

问题描述

i added a sleep timer to close the app after a specified time entered by the user, everything is ok but when the time finishes the app crashes instead of just close. i have a class for the sleep timer and i added the code for closing the app as showing below. it is System.exit(0); is there is another thing to do to avoid the app crash.?

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

when the timer finishes the countdown the app crashes instead of close and I saw below error in 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)** 

in the above error, there is a link for the onStartcommand in the playerservise.JAVA, when i click it it show me below code in the 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;
    }

标签: androidnullsleepcountdowntimerruntimeexception

解决方案


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它。

此外,您应该考虑正确终止您的活动/服务,而不是试图终止该进程。


推荐阅读