I want to use second display in my compact android device with two screen, it already works in Android API 25 but when I try to work it with API 28, I receive an error like this.
My main goal is to check whether display exists, if it exists, show some content on it, if not do nothing. But even the catch block catches the exception, the app crashes.
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: FATAL EXCEPTION: main Process: elbepos.puzzle.pos.de, PID: 13056 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.Display.getDisplayId()' on a null object reference at android.app.ActivityThread.performConfigurationChangedForActivity(ActivityThread.java:4876) at android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:5238) at android.app.ActivityThread$ActivityClientRecord.lambda$init$0(ActivityThread.java:458) at android.app.-$$Lambda$ActivityThread$ActivityClientRecord$HOrG1qglSjSUHSjKBn2rXtX0gGg.onConfigurationChanged(Unknown Source:2) at android.view.ViewRootImpl.performConfigurationChange(ViewRootImpl.java:3899) at android.view.ViewRootImpl.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) at android.view.Choreographer.doCallbacks(Choreographer.java:761) at android.view.Choreographer.doFrame(Choreographer.java:696) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) 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)