我的应用程序的活动具有使它们成为肖像的所有属性:
android:screenOrientation="portrait"
应用程序的 minSdkVersion 为 21。对于 Android 5.0 手机,如果设备使用自动旋转设置并且应用程序进入后台(最近/概览屏幕)然后旋转设备,则应用程序崩溃。
发生这种情况时,这是我从错误中得到的堆栈跟踪:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ActionMenuPresenter.updateMenuView(boolean)' on a null object reference
at android.widget.ActionMenuView.onConfigurationChanged(ActionMenuView.java:133)
at android.view.View.dispatchConfigurationChanged(View.java:9487)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1294)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewGroup.dispatchConfigurationChanged(ViewGroup.java:1299)
at android.view.ViewRootImpl.updateConfiguration(ViewRootImpl.java:3417)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3599)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
问题似乎出在 onCreateOptionsMenu 中。这是我主要活动的简化版:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.menu_mainpages, menu)
val menuItem = menu.getItem(0)
val profilePictureView = LayoutInflater.from(this)
.inflate(R.layout.profile_image_layout,
coordinator_main,
false)
menuItem?.actionView = profilePictureView
return true
}
}