getActivity from fragment inside View pager return null

有些话、适合烂在心里 提交于 2021-02-19 06:15:11

问题


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

回答1:


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

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}



回答2:


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



来源:https://stackoverflow.com/questions/34157262/getactivity-from-fragment-inside-view-pager-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!