savepopulation
savepopulation

Reputation: 11921

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.

Upvotes: 9

Views: 2548

Answers (3)

Mr-IDE
Mr-IDE

Reputation: 7661

The team at Open Whisper Systems encountered the same problem:

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

They think it's caused by a bug in the Android framework, when it calls onNewIntent() very shortly after onCreate(), even if you call finish() from within your onCreate(). This eventually causes null objects to be used in onNewIntent() and then a 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/0b4ea2412273f4e4918cff8f304380d3151ff6d4/src/org/smssecure/smssecure/ConversationActivity.java#L225-L249


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.

Upvotes: 3

Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

If you are getting NullPointerException in onNewIntent() method then probably filtered object is null.

Try this way

@Override
protected void onNewIntent(Intent intent) {

   //Check for null value and then proceed. 
   if(filtered != null)
      filtered.clear();
} 

Upvotes: 0

A Honey Bustard
A Honey Bustard

Reputation: 3503

Keep the lifecycle in mind, maybe you are returning to your Activity from some point where onCreate() is not called (for example if you have set launchMode=singleTask or something in your manifest). Maybe initialize/repopulate your ArrayList in onResume() and also check if it is not null, on Resume() is called for sure when you return to your Activity.

Upvotes: 1

Related Questions