2013-06-19 55 views
0

我有一個錯誤,當我嘗試更新我的FragmentPagerAdapter的數據源,我試圖在這裏尋找,但我不從我的活動沒有發現任何類似FragmentStatePagerAdapter:NullPointerException異常改變時,數據集

代碼

從PageAdapter
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    private List<Noticia> listaNoticias = new ArrayList<Noticia>(); 

// Set the adapters 
    mPager = (ViewPager) findViewById(R.id.pager);   
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
    mPager.setAdapter(mPagerAdapter); 

// Get first page from JSON and show in the pager 
    listaNoticias = Noticia.getPageFromJSON(1); 
    ((ScreenSlidePagerAdapter) mPagerAdapter).setNoticias(listaNoticias); 
    mPagerAdapter.notifyDataSetChanged(); 
} 

代碼

class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter 
{ 
     private List<Noticia> noticias = new ArrayList<Noticia>(); 

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

     public void setNoticias(List<Noticia> listaN) 
     { 
      noticias = new ArrayList<Noticia>(listaN); 
     } 

     @Override 
     public Fragment getItem(int position) 
     { 
      if(position==10) 
      { 
// Get the second page 
        noticias = Entry.addPageFromJSON(noticias, 2); // I manually checked the ArrayList and its content is OK 
        notifyDataSetChanged(); 
      } 
      EntrySlidePageFragment fragmento = new EntrySlidePageFragment(); 
      fragmento.setFragment(noticias.get(position)); 

      return fragmento; 
     } 

     @Override 
     public int getCount() 
     { 
      return noticias.size(); 
     } 

     public int getItemPosition(Object item) 
     { 
      return POSITION_NONE; 
     }  
} 

當我添加新的內容到ArrayList的應用程序停止工作,我得到這些錯誤:

threadid=1: thread exiting with uncaught exception (group=0x40015560) 
FATAL EXCEPTION: main 
java.lang.NullPointerException 
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:119) 
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:829) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:1044) 
at android.support.v4.view.ViewPager.populate(ViewPager.java:911) 
at android.support.v4.view.ViewPager$3.run(ViewPager.java:242) 
at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1757) 
at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1890) 
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:848) 
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691) 
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125) 
at android.app.Activity.dispatchTouchEvent(Activity.java:2096) 
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675) 
at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194) 
at android.view.ViewRoot.handleMessage(ViewRoot.java:1878) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:130) 
at android.app.ActivityThread.main(ActivityThread.java:3683) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:507) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
at dalvik.system.NativeStart.main(Native Method) 

有人知道這裏發生了什麼?

回答

0

我使用打包07/2012 Android的支持-V4的舊版本,並省略notifyDataSetChanged()解決了這個問題由我自己。仍然不知道是什麼導致了錯誤,也許新版本有一些錯誤...