view Pager不会重新加载其内容

时间:2014-04-03 08:22:28

标签: android android-viewpager fragment pager fragmentstatepageradapter

我有单个活动,在onCreate方法中我创建了viewPager,然后我向它提供了我的CustomAdapter(它继承自FragmentStatePagerAdapter)的数据并调用viewPager setCurrentItem。在onPageSelected方法中,我从适配器返回片段实例,然后我调用渲染屏幕的特定方法。这就是问题,它在我执行活动时第一次完美运行,但是当我离开那个活动然后再回到崩溃状态时。任何提示为什么会发生这种情况?似乎第二次适配器只是没有注意到它充满了新的数据。 我尝试强制适配器在onCreate方法中重新加载调用adapter.notifyDataSetChanged(),但它没有解决问题。

这是代码,我希望它能解决问题。

public class DetailActivity extends BaseActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    setContentView(R.layout.detail_activity);
    JSONObject data = null;
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mPosition = 0; 
    try {
        data = new JSONObject(extras.getString("data"));
        } catch (JSONException e) {
        e.printStackTrace();
        }
    mPosition = extras.getInt("position");
    Log.d(DEBUG_TAG, "data from intent "+data.toString());
    mViewPager.setAdapter(
            new QAdapter(getSupportFragmentManager(), data));
    mViewPager.setOnPageChangeListener(this);
    mViewPager.setCurrentItem(mPosition);     
  }
  @Override
  public void onPageSelected(int i) {
Log.d(DEBUG_TAG, "dumping fragments");
    ((QAdapter)mViewPager.getAdapter()).dumpFragments();
    InspectionFragment DetailFragment = ((QAdapter)mViewPager.getAdapter()).getFragment(i);
    DetailFragment.showView();
  }

  public class QAdapter extends FragmentStatePagerAdapter {

    public int mPageCount;
    public JSONObject data;
    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    public QAdapter(FragmentManager fm, JSONObject data) {
        super(fm);
        data = data;
    }

@Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    public DetailFragment getFragment(int key) {
        //return mPageReferenceMap.get(key);
        return (DetailFragment)registeredFragments.get(key);
    }

    public void dumpFragments(){
        //first sparse array
        int nsize = registeredFragments.size();
        int key;
        Log.d(DEBUG_TAG, "DUMP FRAGMENT: nsize is " + String.valueOf(nsize));
        for(int i=0;i<nsize;i++){
            key = registeredFragments.keyAt(i);
            DetailFragment frag = (DetailFragment)registeredFragments.get(key);
            if (frag != null){
                Log.d("MULTI ADAPTER", "FRag in sparse array: in position" +String.valueOf(key) +"#"+ String.valueOf(frag));
            } else {
                Log.d("MULTIADAPTER","nothign here on position"+String.valueOf(key));
            }
        }
        //now iterate internal array with fragmetns
    }

     public DetailFragment getFragment(int key) {
        return (DetailFragment)registeredFragments.get(key);
    }
    @Override
public void destroyItem(ViewGroup container, int position, Object object) {
    super.destroyItem(container, position, object);
        registeredFragments.remove(position);
}
 @Override
    public Fragment getItem(int position) {
        return DetailFragment.newInstance(data, position);
    }

这是stacktrace:

DetailActivity: show page 2, adapterPageCount: 4
DetailActivity﹕ DUMP FRAGMENT: nsize is 0
DetailActivity﹕ inspection fragment is null

java.lang.RuntimeException: Unable to start activity ComponentInfo{......}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        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:791)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
        at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
        at ........ui.DetailActivity.onPageSelected(DetailActivity.java:168)
        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:541)
        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:507)
        at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:488)
        at com.tenclouds.appopolis.ui.InspectionActivity.onCreate(InspectionActivity.java:102)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)

(3第一行是我的日志消息),通过他们来判断我似乎将数据传递给适配器(4个元素),但是它没有创建必要的片段而且它在线上崩溃了DetailFragment.showView(),我不能找出它不创造片段的原因。

0 个答案:

没有答案