我想在我的带有两个屏幕的紧凑型 android 设备中使用第二个显示器,它已经在 Android API 25 中工作,但是当我尝试使用 API 28 时,我收到这样的错误。
我的主要目标是检查显示是否存在,如果存在,则在其上显示一些内容,如果不存在则什么也不做。但即使 catch 块捕获异常,应用程序也会崩溃。
abstract class BaseActivity: AppCompatActivity() {
companion object {
const val PRESENTATION_KEY = "presentation"
const val TAG = "BaseActivity"
}
private lateinit var baseApp: BaseApp
protected var display: Display? = null
private var presentation: BasePresentation? = null
private var mContent: PresentationContent? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i(TAG, "onCreate")
baseApp = application as BaseApp
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configureDisplay(savedInstanceState)
}
}
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun configureDisplay(savedInstanceState: Bundle?) {
if (savedInstanceState != null) {
mContent = savedInstanceState.getParcelable(PRESENTATION_KEY)
}
try {
val displayManager = getSystemService(Context.DISPLAY_SERVICE) as? DisplayManager
val presentationDisplays = displayManager?.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION)
if (presentationDisplays != null && presentationDisplays.isNotEmpty()) {
display = presentationDisplays.first()
}
} catch (e: Exception) {
Log.e(TAG, e.message)
}
}
}
2019-06-24 19:12:00.392 13056-13056/elbepos.puzzle.pos.de E/AndroidRuntime:致命异常:主进程:elbepos.puzzle.pos.de,PID:13056 java.lang.NullPointerException:尝试在 android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:5238) 的 android.app.ActivityThread.performConfigurationChangedForActivity(ActivityThread.java:4876) 的空对象引用上调用虚拟方法“int android.view.Display.getDisplayId()”在 android.app.ActivityThread$ActivityClientRecord.lambda$init$0(ActivityThread.java:458) 在 android.app.-$$Lambda$ActivityThread$ActivityClientRecord$HOrG1qglSjSUHSjKBn2rXtX0gGg.onConfigurationChanged(Unknown Source:2) 在 android.view.ViewRootImpl。在 android.view.ViewRootImpl 执行配置更改(ViewRootImpl.java:3899)。performTraversals(ViewRootImpl.java:2014) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183) at android.view.Choreographer$CallbackRecord.run (Choreographer.java:949) 在 android.view.Choreographer.doCallbacks(Choreographer.java:761) 在 android.view.Choreographer.doFrame(Choreographer.java:696) 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer. java:935) 在 android.os.Handler.handleCallback(Handler.java:873) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在 com.android.internal.os 的 java.lang.reflect.Method.invoke(Native Method)。RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)