如何在Android中的viewPager适配器中修复getCount()

时间:2017-06-04 14:23:41

标签: android android-fragments android-viewpager

我想在我的应用程序中使用viewPager,我想使用这个库:UltraViewPager

我在适配器中写下以下代码:

public class BoxOfficeAdapter extends PagerAdapter {
    private Context context;
    private List<BoxOfficeDatum> model;
    private LayoutInflater inflater;
    private BoxOfficeDateListener listener;

    public BoxOfficeAdapter(Context context, List<BoxOfficeDatum> model, BoxOfficeDateListener listener) {
        this.context = context;
        this.model = model;
        this.listener = listener;
        this.inflater = (LayoutInflater) this.context.getSystemService(this.context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        View view = this.inflater.inflate(R.layout.row_box_office, container, false);

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

但是在运行应用程序时,请显示以下错误:

FATAL EXCEPTION: main
                                                                       Process: com.example.app, PID: 5948
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'int com.tmall.ultraviewpager.UltraViewPagerAdapter.getCount()' on a null object reference
                                                                           at com.tmall.ultraviewpager.UltraViewPagerView.getCurrentItem(UltraViewPagerView.java:220)
                                                                           at com.tmall.ultraviewpager.UltraViewPagerView.onMeasurePage(UltraViewPagerView.java:88)
                                                                           at com.tmall.ultraviewpager.UltraViewPagerView.onMeasure(UltraViewPagerView.java:84)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                           at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                           at com.tmall.ultraviewpager.UltraViewPager.onMeasure(UltraViewPager.java:177)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                           at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.support.v7.widget.CardView.onMeasure(CardView.java:211)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                           at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1420)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                           at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1658)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                           at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6162)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                                                                           at com.android.internal.policy.DecorView.onMeasure(DecorView.java:688)
                                                                           at android.view.View.measure(View.java:19826)
                                                                           at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2335)
                                                                           at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1410)
06-04 16:37:47.075 5948-5948/com.example.app E/AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1660)
                                                                           at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1286)
                                                                           at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6536)
                                                                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
                                                                           at android.view.Choreographer.doCallbacks(Choreographer.java:683)
                                                                           at android.view.Choreographer.doFrame(Choreographer.java:619)
                                                                           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6247)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

我该如何解决?

1 个答案:

答案 0 :(得分:-1)

列表为空,为什么空指针即将到来。 尝试使用不同的方法从响应列表中设置数据

像:

per

BoxOfficeResponse responseBody=response.body();
boxOfficeModel.addAll(responseBody.getBoxOfficeData().getBoxOfficeDatum())