来自View pager内部片段的getActivity返回null

时间:2015-12-08 13:34:13

标签: android android-fragments android-viewpager

我在Fragment Activity里面有一个View分页器,View分页器在活动开始时包含两个片段我有一个休息请求当我得到响应时我想更新每个片段内部和每个片段中的视图我正在使用getActivity但我的问题是我总是在平板电脑设备上的getActivity上获得空指针异常,但我在手机上没有遇到这个问题

这是我的寻呼机适配器

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片段:

  

致命的例外:主要                                                                            过程:android.vi.com.vad,PID:9554                                                                            显示java.lang.NullPointerException                                                                                在   android.vi.com.vad.Guides_fragment.update(Guides_fragment.java:138)                                                                                在   android.vi.com.vad.Guide_activity $ GuideTask.onPostExecute(Guide_activity.java:213)                                                                                在   android.vi.com.vad.Guide_activity $ GuideTask.onPostExecute(Guide_activity.java:165)                                                                                在android.os.AsyncTask.finish(AsyncTask.java:632)                                                                                在android.os.AsyncTask.access $ 600(AsyncTask.java:177)                                                                                在   android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:645)                                                                                在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                在android.os.Looper.loop(Looper.java:149)                                                                                在android.app.ActivityThread.main(ActivityThread.java:5234)                                                                                at java.lang.reflect.Method.invokeNative(Native Method)                                                                                在java.lang.reflect.Method.invoke(Method.java:515)                                                                                在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)                                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)                                                                                在dalvik.system.NativeStart.main(本地方法)

这是我在加载数据时从活动内部调用它的方法

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

这时我将值设置为活动:

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

2 个答案:

答案 0 :(得分:1)

我通过在Fragment onCreate()中调用setRetainInstance(true)解决了我的问题

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

答案 1 :(得分:0)

为什么需要调用getActivity?活动是作为参数传递的?即

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

话虽如此,如果您正在编写最新的API级别onAttachActiviy(Activity)已被弃用,您应该使用onAttachActivity(Context)

请参阅Android Fragment onAttach() deprecated