Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android service crash when use kotlin language

I am writing an android app with firebase and kotlin. Now I created a kotlin class that extends Service class.

The problem is, after start service if I close the app it crashed!

If I write same code with java all working fine! I can't understand why it crashed when I use kotlin?

Look my code bellow.

myService.kt

class myService : Service() {
    internal var TAG = "tagForLogD"

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG, "myService Created!")
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        return Service.START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "myService destroyed")
    }
}

myService.java

public class myService extends Service {
    String TAG = "tagForLogD";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate(){
        super.onCreate();
        Log.d(TAG, "myService Created!");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "myService destroyed");
    }
}

Thank you for your support.

Logcat throw bellow exception

03-10 15:13:53.847 4911-4911/net.beingup.simplechat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: net.beingup.simplechat, PID: 4911
    java.lang.RuntimeException: Unable to start service net.beingup.simplechat.Notification.myService@11a4382f with null: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter intent
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3975)
        at android.app.ActivityThread.access$2300(ActivityThread.java:218)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1842)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:7007)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
     Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter intent
        at net.beingup.simplechat.Notification.myService.onStartCommand(myService.kt)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3958)
like image 561
shariful islam Avatar asked Mar 10 '19 07:03

shariful islam


2 Answers

You declared intent to be non-null in onStartCommand:

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int

Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter intent

So instead declare it as Intent?.

Reasons that the passed Intent would be NULL in onStartCommand

like image 142
tynn Avatar answered Oct 20 '22 11:10

tynn


If you are using a LifecycleService then this issue was fixed in version 2.2.0-alpha02 of AndroidX Lifecycle library.

You can override:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int

(Notice: Intent? instead of Intent)

like image 36
Malwinder Singh Avatar answered Oct 20 '22 10:10

Malwinder Singh