Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onActivityResult. Member variable is sometimes null

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

public abstract class SuperActivity extends Activity

Activity 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

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:

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.

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.

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.

Thanks so much!

like image 213
metter Avatar asked Apr 29 '12 15:04

metter


1 Answers

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!

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.

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.

like image 53
WallyHale Avatar answered Oct 31 '22 04:10

WallyHale