Android onActivityResult。成员变量有时为空

时间:2023-01-25 21:40:58

I've written an App and now, post release, some of my users seem to be experiencing a nasty bug. One that I am simply unable to reproduce.

我写了一个应用程序,现在发布后,我的一些用户似乎遇到了一个严重的问题。一个我无法复制的。

Involved in this are three Activities.

这包括三个活动。

Activity 1: SuperActivity

活动1:超活性

public abstract class SuperActivity extends Activity

Activity 2: MainActivity

活动2:MainActivity

public class MainActivity extends SuperActivity {

  private MyObject myMemberVariable;    

  @Override
  public void onCreate(Bundle savedInstanceState) {

    //Get the intent that started this activity. I am always providing Extras, so this is never null.
    Bundle bundle = getIntent().getExtras();

    //Set stuff in myMemberVariable from these extras. This never fails either.
    myMemberVariable = BundleLoader.loadFromBundle(bundle);

  }


  @Override
  public void onListDialogClick(int requestCode, String[] options, int position, String extra) {
    //THIS is the place where, according to my stacktrace from the DeveloperConsole, the null pointer arises.
    myMemberVariable.setXY(position);
  }

Activity 3: ListDialogActivity

活动3:ListDialogActivity

public class ListDialogActivity extends SuperActivity

What I'm doing here is I start the ListDialogActivity from MainActivity to present the user with a few choices. Once he made a selection, the ListDialogActivity will set a result and then finish. In the SuperActivity I've got:

在这里,我从MainActivity开始ListDialogActivity,向用户展示一些选项。一旦他做出选择,ListDialogActivity将设置一个结果,然后完成。在超级活动中,我得到:

onActivityResult(int requestCode, int resultCode, Intent data) {
  //if activityRequestCode was ListDialogRequestCode and the Result was RESULT_OK, then call onListDialogClick(..) with the appropriate values.
}

So this is how the onListDialogClick() in my MainActivity is invoked (So you can just think of it as a normal onActivityResult()). There, SOME few users experience a null pointer exception because myMemberVariable is null.

这就是调用我的MainActivity中的onListDialogClick()的方式(因此您可以把它看作一个普通的onActivityResult())。在那里,一些用户会遇到空指针异常,因为myMemberVariable是空的。

I cannot figure out what is happening here. Also, I am saving my instance state to handle phone rotations, but even if I did not, since the onCreate would be recalled, it still would not be null, it would only have lost it's state and been recreated from the initial Intent.

我不知道这里发生了什么。另外,我保存了实例状态来处理电话旋转,但是即使我没有,因为onCreate将被重新调用,它仍然不会是空的,它只会丢失它的状态并从最初的意图中重新创建。

Can anyone please explain to me how this is possible and whats happening here? Did I missunderstand something from Android? Why is it only happening to a few people? What could be the reason for this? I'd be really gratefull for even the tiniest suggestion.

谁能给我解释一下这是怎么可能的,这是怎么回事?我是否误解了Android?为什么它只发生在少数人身上?为什么会这样呢?即使是最微小的建议,我也非常感激。

Thanks so much!

非常感谢!

1 个解决方案

#1


11  

zapl is correct in that you have to save the values in onSaveInstanceState, and in my case I am restoring in onCreate() which runs BEFORE onActivityResult, so no more NullPointerExceptions!

zapl是正确的,因为您必须将值保存在onSaveInstanceState中,在我的例子中,我正在还原onCreate(),它在onActivityResult之前运行,这样就不会再有nullpointerexception了!

You have probably sorted this by now, but I was having oodles of trouble with the same issue. My app starts the camera to take a photo, and then copy the captured file into the currentDirectory in the app. Occasionally when the photo was taken I was getting NullPointerExceptions in onActivityResult.

你可能已经把这个整理好了,但是我在同样的问题上有很多麻烦。我的app启动相机拍照,然后将捕获的文件复制到app的currentDirectory中。有时候拍照时,onActivityResult中会出现nullpointerexception。

It took me a while to realise the activity must have been getting killed by Androids memory management, and recreated, thus losing all my member variables. This wasn't obvious (hats off to Android for such a seamless kill / restore!). This was happening on an HTC Desire HD, and was so random it was very hard to track down.

我花了一段时间才意识到这个活动一定是被android内存管理杀死的,然后重新创建,从而丢失了所有的成员变量。这并不明显(为了如此无缝的杀伤/恢复,请向Android致敬!)这是在HTC Desire HD上发生的,而且非常随机,很难找到。

#1


11  

zapl is correct in that you have to save the values in onSaveInstanceState, and in my case I am restoring in onCreate() which runs BEFORE onActivityResult, so no more NullPointerExceptions!

zapl是正确的,因为您必须将值保存在onSaveInstanceState中,在我的例子中,我正在还原onCreate(),它在onActivityResult之前运行,这样就不会再有nullpointerexception了!

You have probably sorted this by now, but I was having oodles of trouble with the same issue. My app starts the camera to take a photo, and then copy the captured file into the currentDirectory in the app. Occasionally when the photo was taken I was getting NullPointerExceptions in onActivityResult.

你可能已经把这个整理好了,但是我在同样的问题上有很多麻烦。我的app启动相机拍照,然后将捕获的文件复制到app的currentDirectory中。有时候拍照时,onActivityResult中会出现nullpointerexception。

It took me a while to realise the activity must have been getting killed by Androids memory management, and recreated, thus losing all my member variables. This wasn't obvious (hats off to Android for such a seamless kill / restore!). This was happening on an HTC Desire HD, and was so random it was very hard to track down.

我花了一段时间才意识到这个活动一定是被android内存管理杀死的,然后重新创建,从而丢失了所有的成员变量。这并不明显(为了如此无缝的杀伤/恢复,请向Android致敬!)这是在HTC Desire HD上发生的,而且非常随机,很难找到。