gpt4 book ai didi

android - 在 Kotlin 中将单例服务转换为对象会使我的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 22:59:49 28 4
gpt4 key购买 nike

我有一个对象,我将启动为:

public class SIPManager
extends Service
implements OnPortSIPEvent
{

private static SIPManager instance;

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

我会在我的应用程序中开始我会这样做:

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)

我的单例现在看起来像这样:

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() 但仍然只是崩溃

最佳答案

MY Singleton now looks like this

您不能使 Android 服务成为 Kotlin 对象。 Kotlin 无法创建 Service 实例;只有 Android 框架可以。

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

关于android - 在 Kotlin 中将单例服务转换为对象会使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57076148/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com