重新创建活动时,viewpager适配器为null

时间:2014-11-17 20:31:06

标签: android android-viewpager adapter

我正在实现包含viewpager的android应用程序。问题是当应用程序在后台被杀死时,当我使用asynctask onPostExecute方法中的适配器实例设置我的viewpager时,我在行上有空指针。下面是我的代码。希望得到帮助:)

崩溃发生的片段类内的片段

protected void onPostExecute(ArrayList<CategoryChild> result) {

            super.onPostExecute(result);

            if (this.fragmentWeakRef.get() != null) {

                if (result != null) {
                    mHomeCategories = result;

                    if(mBannerViewPager != null){
                    mBannerViewPager.setAdapter(new BannerViewpagerAdapter(
                            getChildFragmentManager(), getActivity()
                                    .getApplicationContext())); // here app crashes after being  killed in background
                                            //  and restored 

                    }
                    mcategoryViewPager.setAdapter(new CategoryViewpagerAdapter(
                            getChildFragmentManager(), getActivity()
                                    .getApplicationContext(),setFragmentsArray()));

                } else {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "No Internet Connection", Toast.LENGTH_SHORT)
                            .show();
                }

            }
        }
    }   

这是我的适配器

public class BannerViewpagerAdapter extends FragmentPagerAdapter {

    private SparseArray<BannerFragment> registeredFragments = new SparseArray<BannerFragment>();
    private static HashMap<Integer, Integer> myHashmap = new HashMap<Integer, Integer>();

    static {
        myHashmap.put(0, R.drawable.img);
        myHashmap.put(1, R.drawable.stopex);
    }

    public BannerViewpagerAdapter(android.support.v4.app.FragmentManager fm, Context context) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        BannerFragment myFragment = null;

        myFragment = registeredFragments.get(position);

            if(myFragment ==  null){
                myFragment = BannerFragment.newInstance(myHashmap.get(position));
                registeredFragments.put(position, myFragment);
            }


        return myFragment;
    }

    @Override
    public Object instantiateItem(ViewGroup arg0, int arg1) {
        // TODO Auto-generated method stub
        return super.instantiateItem(arg0, arg1);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

}

1 个答案:

答案 0 :(得分:0)

这是合乎逻辑的。活动被杀死,其成员也被杀死。因此,在这种情况下,您应该解决应用程序的行为。您还可以添加一个验证您的ViewPager存在的方法,就像您上面的某些行一样:

if (mcategoryViewPager != null) {
        mcategoryViewPager.setAdapter(new CategoryViewpagerAdapter(
                getChildFragmentManager(), getActivity()
                      .getApplicationContext(),setFragmentsArray()))
}

喔。您的活动被终止,因此getActivity()将返回null。所以,只需在之前添加:

if (getActivity() == null) { return; }