2

I have 4 fragments in a ViewPager in an activity. In one of the fragments I start an activity for result, and always get a crash when coming back to the first activity. The error is:

Process: com.mycom.android.myapp.debug, PID: 13505
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196610, result=0, data=Intent {  }} to activity {com.mycom.android.myapp.debug/com.mycom.android.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference
            at com.mycom.android.myapp.fragment.AgendaFragment.onActivityResult(AgendaFragment.java:415)

Although I tried commenting out the body of onActivityResult function of the fragment, still happens. It looks like the fragment is not restored in time.

How should I go about dealing with this? tks

Update: this is my onActivityResult in the problematic fragment. There are child fragments and I'm sure there are @Override in those fragments as well. I try commenting the body of the loop, still no luck. Any more ideas? thanks.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    LOGD(TAG, "onActivityResult called BEFORE calling super");

    super.onActivityResult(requestCode, resultCode, data);
    LOGD(TAG, "onActivityResult called AFTER calling super");
    /**we need to broadcast this event to nested child fragments */
    List<Fragment> fragments = getChildFragmentManager().getFragments();
    if (!CollectionUtils.isEmpty(fragments)) {
        for (Fragment fragment : fragments) {
            if(fragment!=null){
                LOGD(TAG, "fragment not null");
                fragment.onActivityResult(requestCode, resultCode, data);

            }else
                LOGD(TAG,"fragment IS null");

        }
    }

}

Update 2: since I startActivity() in a nested fragment of AgendaFragment, following pvshnik's answer at onActivityResult() not called in new nested fragment API Inside ListFragment, which is a child fragment of AgendaFragment, I did

getParentFragment().startActivityForResult(intent, requestCode);
Community
  • 1
  • 1
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126
  • are you overriding the onActivityResult function, the error indicates that the virtual method is being called. add @Override to the method if you do not have it. – faljbour Mar 26 '15 at 07:02
  • Yes, and I did put @Override already, just updated my code above. – EyeQ Tech Mar 26 '15 at 07:07
  • so is line 415 on your `AgendaFragment` this `fragment.onActivityResult(requestCode, resultCode, data);`? – kha Mar 26 '15 at 09:24
  • @kha no, it's the start of the function `public void onActivityResult(int requestCode, int resultCode, Intent data)` – EyeQ Tech Mar 26 '15 at 12:36
  • @TungMaiLe Is the piece of code you've posted belong to `AgendaFragment.onActivityResult`? please show how do you start activity – nikis Mar 26 '15 at 12:46
  • @nikis thanks for asking, updated my question above – EyeQ Tech Mar 26 '15 at 12:52

1 Answers1

1

You can try like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    List<Fragment> listOfFragments = getChildFragmentManager().getFragments();

    if(listOfFragments.size()>=1){
        for (Fragment fragment : listOfFragments) {
            if(fragment instanceof yourFragmentName){
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

    super.onActivityResult(requestCode, resultCode, data);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Sai's Stack
  • 1,345
  • 2
  • 16
  • 29