使用Kotlin语言时Android服务崩溃

时间:2019-03-10 07:27:56

标签: android kotlin service

我正在用Firebase和Kotlin编写一个Android应用程序。现在,我创建了一个扩展Service类的 kotlin类

问题是,启动服务后,如果我关闭应用程序,则该应用程序崩溃了

如果我使用 java 编写相同的代码,则一切正常!我不明白为什么使用 kotlin 时会崩溃?

看下面的代码。

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

感谢您的支持。

Logcat引发波纹管异常

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)

3 个答案:

答案 0 :(得分:5)

您在intent中声明onStartCommand为非空:

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

指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数意图

因此,将其声明为Intent?

Reasons that the passed Intent would be NULL in onStartCommand

答案 1 :(得分:2)

如果您使用的是LifecycleService,则此问题是AndroidX Lifecycle库的2.2.0-alpha02版本中的fixed

您可以覆盖:

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

(注意:Intent?而非Intent

答案 2 :(得分:0)

Google 在 androidx.lifecycle:lifecycle-service:2.2.0 中修复了这个错误;

code