EyeQ Tech
EyeQ Tech

Reputation: 7358

onActivityResult in ViewPager fragment crash

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);

Upvotes: 2

Views: 3533

Answers (1)

Sai&#39;s Stack
Sai&#39;s Stack

Reputation: 1385

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);
}

Upvotes: 1

Related Questions