Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity onNewIntent Null Pointer Exception

I initialize my lists in my activity onCreate() like below:

private List<MyItem> filtered;
@Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_dashboard);
      filtered = new ArrayList<>();

      // more things
}

And when i try to use filtered items from onNewIntent sometimes i get a null pointer exception.

 @Override
   protected void onNewIntent(Intent intent) {
      super.onNewIntent(intent);
      filtered.clear();
   }

How could it be possible?

Edit: My Activity's launchmode is SingleTask

Edit 2:

I cannot send more useful logs because this crash is in production. Only i get some fabric logs.

Thanks for your help but i cannot paste whole code cause of privacy.

I think i have a problem on SingleTask-OnCreate-OnNewIntent usage. Simply i'm trying to open my app from notification with a parameter decides which fragment will be opened when user navigates to activity.

Do you have any examples about this which contains SingleTask-OnCreate-OnNewIntent implementations?

Thanks to all for help.

like image 295
savepopulation Avatar asked Sep 24 '15 09:09

savepopulation


1 Answers

The team at Open Whisper Systems encountered the same problem:

https://github.com/WhisperSystems/Signal-Android/issues/2971

They think it's caused by the Android framework calling onNewIntent() very shortly after onCreate(), even if you call finish() from within your onCreate(). This eventually causes null objects being used in onNewIntent() and then NullPointerException, because the objects haven't been set up in the usual onCreate(). It seems to be a rare scenario or a race condition between onCreate() and onNewIntent().

They seem to have fixed it by checking for isFinishing() in onNewIntent() to prevent onNewIntent() from continuing:

@Override
protected void onNewIntent(Intent intent) {
    Log.w(TAG, "onNewIntent()");

    if (isFinishing()) {
        Log.w(TAG, "Activity is finishing...");
        return;
    }
    ...
}

Source 1: https://github.com/SilenceIM/Silence/commit/c0acae1124c0c066fd961d21d3ec9b989574125d

Source 2: https://github.com/SilenceIM/Silence/blob/master/src/org/smssecure/smssecure/ConversationActivity.java#L223


Update: 10 November 2017: This problem seems to happen much less often when upgrading the build tools in app/build.gradle:

buildToolsVersion '25.0.2'

It seemed to happen more often with older build tools, like buildToolsVersion '23.0.3', but the fix is still needed, as it still happens on rare occasions.

like image 70
Mr-IDE Avatar answered Oct 25 '22 13:10

Mr-IDE