2

I have a View pager inside of Fragment Activity and the View pager Contain two fragment at the start of the activity i have a rest request and when i get the response i want to update the Views inside of each fragment and in each one i'm using getActivity but my problem is the i always get null pointer exception on getActivity just on tablet Devices but i didn't get this issue on mobile phones

that's my pager adapter

public class Guide_ViewPager_adapter extends FragmentStatePagerAdapter {
    private Guides_fragment guide_frag = new Guides_fragment();
    private Maps_fragment maps_frag = new Maps_fragment();

    public Guide_ViewPager_adapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return guide_frag;
            case 1:
                return maps_frag;
        }
        return  null;

    }

    @Override
    public int getCount() {
        return 2;
    }
}

LogCat snippet:

FATAL EXCEPTION: main Process: android.vi.com.vad, PID: 9554 java.lang.NullPointerException at android.vi.com.vad.Guides_fragment.update(Guides_fragment.java:138) at android.vi.com.vad.Guide_activity$GuideTask.onPostExecute(Guide_activity.java:213) at android.vi.com.vad.Guide_activity$GuideTask.onPostExecute(Guide_activity.java:165) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:149) at android.app.ActivityThread.main(ActivityThread.java:5234) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) at dalvik.system.NativeStart.main(Native Method)

This is the method that i call it from inside the activity when the data loaded

public void update(String result) {
    if (animation != null) {
        animation.reset();
        animation.cancel();
    }
    if (loaderImage != null) {
        loaderImage.clearAnimation();
        loaderImage.setVisibility(View.GONE);
    }
    if (result.equalsIgnoreCase("null")) {
        if (errorImage != null) {
            errorImage.setVisibility(View.VISIBLE);
        }
    } else {
        Guide_GridView_adapter adapter = new Guide_GridView_adapter(activity.getApplicationContext(), Guide_activity.Downloads_Guides);
        guides_gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}

and here when I set value to activity:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.activity = getActivity();
}
Mike
  • 4,550
  • 4
  • 33
  • 47
Antwan
  • 3,837
  • 9
  • 41
  • 62

2 Answers2

1

I have solved my problem by calling setRetainInstance(true) in Fragment onCreate()

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}
Antwan
  • 3,837
  • 9
  • 41
  • 62
  • 1
    Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated: onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity). onCreate(Bundle) will not be called since the fragment is not being re-created. onAttach(Activity) and onActivityCreated(Bundle) will still be called. – Sinh Phan Jun 17 '17 at 04:27
0

Why do you need to call getActivity? The activity is passed in as a parameter? ie

@Override
public void onAttach(Activity activity) {
  super.onAttach(activity);
  this.activity = activity;
}

Having said that if you are coding to the latest API levels onAttachActiviy(Activity) is deprecated and you should be using onAttachActivity(Context) instead

see Android Fragment onAttach() deprecated

Community
  • 1
  • 1
QuantumTiger
  • 970
  • 1
  • 10
  • 22
  • i have tried to use this.activity = activity; but i got same result ,also i couldn't fine overrride method onAttachActivity(Context) – Antwan Dec 08 '15 at 14:40
  • Are you using import android.support.v4.app.Fragment; ? – QuantumTiger Dec 08 '15 at 14:46
  • I'd be inclined to check that your gradle dependencies are using the latest support libraries versions. Also in your original question, where exactly are you seeing the NPE? In the onAttach? or later when you refer to the activity?If so post the function with the NPE – QuantumTiger Dec 08 '15 at 15:21
  • compile 'com.android.support:appcompat-v7:22.2.1' 'com.android.support:support-v4:22.2.1' – Antwan Dec 08 '15 at 15:22
  • I'm using this version of support library – Antwan Dec 08 '15 at 15:24
  • i got NPE on activity here activity.getApplicationContext() – Antwan Dec 08 '15 at 15:24
  • also i tried to use getActivity here but same result – Antwan Dec 08 '15 at 15:25
  • It looks like to use onAttachActivity (Context) you need at least v4:23.0.0 or later. And activity.getApplicationContext() should work if called after onActivityCreated(), but may fail before that - see lifecycle diagram in docs http://developer.android.com/intl/es/guide/components/fragments.html – QuantumTiger Dec 08 '15 at 15:36
  • i don't use onAttachActivityContext and also activity.getApplicationContext() working on mobiles so why its not working on tablets? – Antwan Dec 08 '15 at 15:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97304/discussion-between-quantumtiger-and-tony). – QuantumTiger Dec 08 '15 at 15:42