如何安全地更新ViewPager的模型

时间:2015-07-03 05:44:15

标签: android android-viewpager

我正在使用ViewPager来显示包含引号及其作者的简单片段。我希望能够在用户距离列表末尾4引号时向视图寻呼机后面的ArrayList添加更多引号。通过向web api发出HTTP请求来获取新引号。引号是在正确的时间获取的,但当我尝试在ViewPager的适配器上调用notifyDataSetChanged()时,我得到以下异常:

  

java.lang.NullPointerException:尝试调用虚拟方法'android.support.v4.app.FragmentTransaction android.support.v4.app.FragmentTransaction.add(int,android.support.v4.app.Fragment)'on空对象引用               在android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:119)               在android.support.v4.view.ViewPager.addNewItem(ViewPager.java:836)               在android.support.v4.view.ViewPager.populate(ViewPager.java:1052)               在android.support.v4.view.ViewPager.populate(ViewPager.java:918)               在android.support.v4.view.ViewPager $ 3.run(ViewPager.java:248)               在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)               在android.view.Choreographer.doCallbacks(Choreographer.java:580)               在android.view.Choreographer.doFrame(Choreographer.java:549)               在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)               在android.os.Handler.handleCallback(Handler.java:739)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:135)               在android.app.ActivityThread.main(ActivityThread.java:5257)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

向ViewPager模型添加更多数据的正确方法是什么?或者是否应为新数据创建新的ViewPager?

参考代码:

public class QuotesPageActivity extends FragmentActivity {

    private ViewPager mViewPager;
    private ArrayList<Quote> mQuotes;

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

        //Allows thread to make call to api not on the network in HttpRequestTask
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        //Set up view Pager
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.viewPager);
        setContentView(mViewPager);

        // Get list of quotes
        mQuotes = QuoteCollection.get(this).getQuotes();

        //Set up fragment manager
        FragmentManager fm = getSupportFragmentManager();

        //Configure adapter
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
            @Override
            public Fragment getItem(int position) {
                // Get more quotes if nearing the end
                if (position > mQuotes.size() - 5) {
                    getMoreQuotes();
                }

                return QuotesPageFragment.newInstance(position);
            }

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

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        });
    }

    public void getMoreQuotes() {
        // Fetch 5 more quotes from web api and add them to mQuotes
        QuoteCollection.get(this).addMoreQuotes(5);
        mViewPager.getAdapter().notifyDataSetChanged();
    }
}

0 个答案:

没有答案