在Kotlin中将Singleton Service转换为对象使我的应用程序崩溃

时间:2019-07-17 12:43:40

标签: android kotlin service initialization singleton

我有一个对象会初始化为:

public class SIPManager
    extends Service
    implements OnPortSIPEvent
{

private static SIPManager instance;

public static SIPManager getInstance()
    {
        return instance;
    }
}

然后我将在我的APP中启动:

if (SIPManager.getInstance() != null && callStrategy != null && callStrategy == CallStrategy.SIP) {
    if (SIPManager.getInstance().isSipRegistered && (SIPManager.getInstance().isLoginInProgress || SIPManager.getInstance()
                                    .inActiveCall()!! || SIPManager.getInstance().isRegistrationRefreshValid!!)) {
                       ..... do stuff
    }
}

但是转换为Kotlin之后,我得到了:

2019-07-17 14:39:51.517 21173-21173/com.xelion.android.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xelion.android.debug, PID: 21173
java.lang.ExceptionInInitializerError
    at com.xelion.android.App$Companion.initServices(App.kt:103)
    at com.xelion.android.App.onCreate(App.kt:41)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5920)
    at android.app.ActivityThread.access$1100(ActivityThread.java:200)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    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.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
    at android.content.ContextWrapper.getSystemService(ContextWrapper.java:728)
    at com.xelion.android.sip.SIPManager.onCreate(SIPManager.kt:316)
    at com.xelion.android.sip.SIPManager.<clinit>(SIPManager.kt:126)
    at com.xelion.android.App$Companion.initServices(App.kt:103) 
    at com.xelion.android.App.onCreate(App.kt:41) 
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1154) 
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5920) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:200) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6718) 
    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) 
2019-07-17 14:39:55.561 21173-21173/com.xelion.android.debug 
E/CrashlyticsCore: Failed to execute task.
java.util.concurrent.TimeoutException
    at java.util.concurrent.FutureTask.get(FutureTask.java:206)
    at com.crashlytics.android.core.CrashlyticsBackgroundWorker.submitAndWait(CrashlyticsBackgroundWorker.java:41)
    at com.crashlytics.android.core.CrashlyticsController.handleUncaughtException(CrashlyticsController.java:321)
    at com.crashlytics.android.core.CrashlyticsController$6.onUncaughtException(CrashlyticsController.java:301)
    at com.crashlytics.android.core.CrashlyticsUncaughtExceptionHandler.uncaughtException(CrashlyticsUncaughtExceptionHandler.java:42)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1068)
    at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1063)
    at java.lang.Thread.dispatchUncaughtException(Thread.java:1955)

MY Singleton现在看起来像这样:

object SIPManager : Service(), OnPortSIPEvent {
       override fun onCreate() {

    super.onCreate()

    portSipSdk = PortSipSdk()
    incomingCallToneGenerator = IncomingCallToneGenerator()

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val channel = NotificationChannel(channelID, "Xelion", NotificationManager.IMPORTANCE_DEFAULT)
        channel.enableLights(true)
        notificationManager.createNotificationChannel(channel)
    }
    showServiceNotifiCation()

    subscribeToCallsNotifications()
    }
}

我没有初始化。我应该为我的单身人士创建吗?

我尝试从App.kt类中调用第一件事。 SIPManager.onCreate()但仍然崩溃

1 个答案:

答案 0 :(得分:1)

  

我的辛格尔顿现在看起来像这样

您不能将Android服务作为Kotlin object。 Kotlin无法创建Service实例;只有Android框架可以。

object中的class更改为SIPManager。然后,恢复在Java代码中必须进行的startService()调用,以在需要之前的某个时刻启动SIPManager