9

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.

savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • 1
    Post your logcat if you encounter a `NullPointerException`! Like that I'll just guess `filtered` is null in `onNewIntent()`... – shkschneider Sep 24 '15 at 09:17
  • @savepopulation Has the accepted answer helped you in any way? I am having a similar issue, and am not able to reproduce the issue. Can only see fabric logs of the crashes :( – jayeshsolanki93 Dec 19 '17 at 14:12
  • sorry i left the project i'm having this issue so i cannot see whats happening with build tools 25.0.2 in production. I made a few researches about this issue and according to what i found, decided to accept @Mr-IDE's answer. – savepopulation Dec 19 '17 at 14:14
  • 1
    i think this happens when you define a launch mode -except standart- to your activity and when it's finishing you try to start it again. So it's reasonable to check if it's finishing. i hope this'll help you. – savepopulation Dec 19 '17 at 14:16
  • Ok, thanks. That was useful. – jayeshsolanki93 Dec 19 '17 at 18:23

3 Answers3

3

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.

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
  • About your 10 nov. 2017 update, why are you assuming this ? I think it may be related to the appcompat version more than the build tools version , not ? – crgarridos Nov 12 '17 at 16:15
  • 1
    @crgarridos, Through app monitoring and analytics, it can be seen that updating the build tools version reduces the number of times this bug happens by a large amount. But it doesn't 100% fix it. – Mr-IDE Nov 12 '17 at 23:02
1

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.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38
  • my activity's launchmode is single task. in which kind of situations it can be possible new intent gets called without onCreate? can you give an example? thanks. – savepopulation Sep 24 '15 at 09:33
  • f.e. if you have switch to another Activity and return to your singleTask Activity, onNewIntent() is called and onCreate() is not called – A Honey Bustard Sep 24 '15 at 09:36
  • thats correct but onCreate will be called if single task activity is destroyed. if it's not destroyed my lists will not be null. is it not right? – savepopulation Sep 24 '15 at 09:40
  • Well, your ArrayList maybe shouldn't be null, but it obviously is, so you have to either provide more code or better do some debugging yourself to find on which point the ArrayList becomes null. What do you mean with 'onCreate will be called if single task activity is destroyed' ? – A Honey Bustard Sep 24 '15 at 09:47
  • i cannot catch how this error occurs. That's the problem. And i get logs from fabric. I get this error in production. – savepopulation Sep 24 '15 at 09:49
0

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();
} 
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40