尝试更新 FragmentPagerAdapter 的数据源时出现错误,我尝试在此处搜索,但没有找到类似的东西
我的活动中的代码
@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();
}
来自 PageAdapter 的代码
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)
有人知道这里发生了什么吗?