2017-06-04 82 views
-3

我想在我的應用程序中使用viewPager,我想使用這個庫:UltraViewPager如何解決在viewPager適配器getCount將()在Android的

我下面寫的代碼在適配器:

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) 

我怎樣才能解決呢?

+0

您還沒有通過的時候'onCreateView尋呼機相關的適配器() '結束。那麼UltraViewPager似乎需要一個適配器。 – CommonsWare

+0

@CommonsWare,你可以給我發送代碼嗎?請。我是業餘的。請給我的朋友 –

+0

UltraViewPagerView.java的第220行是什麼? –

回答

-1

列表爲null表示爲什麼空指針即將到來。 嘗試不同的方法從響應數據設置爲您的列表

像:

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

for(int i=0; i<responseBody.getBoxOfficeData().getBoxOfficeDatum(); i++){ 
boxOfficeModel.add(responseBody.getBoxOfficeData().getBoxOfficeDatum().get(i)) 
} 
+0

**如果列表爲空** 如果(boxOfficeModel!= NULL){ boxOfficeAdapter.notifyDataSetChanged(),您可以記錄錯誤; boxOfficeViewPager.setAdapter(boxOfficeAdapter); } else { Log.i(「Error」,「null list」) } – Pamuj

相關問題