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)
誰かがここで何が起こっているか知っていますか?