简体   繁体   English

android:应用程序崩溃,如果方向在子活动中发生变化

[英]android: application crashes if orientation changes in a subactivity

My main activity handles changes into portrait mode just fine. 我的主要活动处理纵向模式的变化就好了。 However, if I launch a very simple activity via startActivityForResult() and switch to portrait mode before finishing it, my main activity crashes because some of its variables are set to null. 但是,如果我通过startActivityForResult()启动一个非常简单的活动并在完成它之前切换到纵向模式,我的主要活动会崩溃,因为它的一些变量设置为null。

It looks like my connection to the Service is not being recreated. 看起来我的服务连接没有重新创建。 I tried manually calling doUnbindService(); doBindService(); 我试着手动调用doUnbindService(); doBindService(); doUnbindService(); doBindService(); inside of onActivityResult() but onServiceConnected() is still not being called. onActivityResult()内部,但onServiceConnected()

What's the proper way to handle the Activity lifecycle here? 在这里处理Activity生命周期的正确方法是什么?

EDIT : I see in the log file that onServiceConnected() is indeed being called -- but it's being called after onActivityResult() . 编辑 :我在日志文件中onServiceConnected()确实正在调用onServiceConnected() - 但它是 onActivityResult() 之后调用的。

For you second Activity add this 为你第二个活动添加这个

  if your android:targetSdkVersion="12" or less
     android:configChanges="orientation|keyboardHidden">

      if your  android:targetSdkVersion="13" or more
  android:configChanges="orientation|keyboardHidden|screenSize">

in manifest.xml 在manifest.xml中

The problem was that I was referring to objects which don't exist until onCreate() is called again. 问题是我指的是在再次调用onCreate()之前不存在的对象。 I needed to queue my answers locally and wait for those objects to be recreated to avoid crashes. 我需要在本地排队我的答案并等待重新创建这些对象以避免崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM