我在我的Application类的onCreate方法中有以下代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getSystemService(Context.MIDI_SERVICE).also {
MidiController.instance.midiManager = it as MidiManager
}
}
当我在设备上测试此代码时,它没有任何问题,但是某些用户设备报告以下错误
java.lang.RuntimeException:
at android.app.ActivityThread.handleBindApplication (ActivityThread.java:4973)
at android.app.ActivityThread.-wrap1 (ActivityThread.java)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1560)
at android.os.Handler.dispatchMessage (Handler.java:111)
at android.os.Looper.loop (Looper.java:207)
at android.app.ActivityThread.main (ActivityThread.java:5765)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679)
Caused by: kotlin.TypeCastException:
我怀疑问题可能是因为我是从Application类而不是从Activity调用getSystemService,这是不好的做法吗?如果不是,可能是什么问题?谢谢
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI)) {
getSystemService(Context.MIDI_SERVICE).also {
MidiController.instance.midiManager = it as MidiManager
}
}