第二次调用setAdapter()后ViewPager不刷新View

时间:2012-11-20 08:52:24

标签: android android-viewpager viewpagerindicator

我的ViewPager有问题。当Activity启动时,我运行AsyncTask从服务器下载数据,我打电话给pager.setAdapter(currentPager) currentPager为空片段,标有ViewPageIndicator.TabPageIndicator

pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(currentPager);

indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);

当AsyncTask完成下载数据时,我调用pager.setAdapter(currentPager);,前2页为空,其他页面都可以。如何首先刷新具有适当数据的页面? 我不能在没有pager.setAdapter(currentPager);的情况下调用setContentView,因为我有一个NULL指针

E/AndroidRuntime(23263): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.windmobile.rbt.android/com.windmobile.rbt.android.category.CategoryActivity}: java.lang.IllegalStateException: ViewPage
r does not have adapter instance.
E/AndroidRuntime(23263):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
E/AndroidRuntime(23263):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
E/AndroidRuntime(23263):        at android.app.ActivityThread.access$2200(ActivityThread.java:119)
E/AndroidRuntime(23263):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
E/AndroidRuntime(23263):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(23263):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(23263):        at android.app.ActivityThread.main(ActivityThread.java:4363)
E/AndroidRuntime(23263):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23263):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(23263):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
E/AndroidRuntime(23263):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
E/AndroidRuntime(23263):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(23263): Caused by: java.lang.IllegalStateException: ViewPager does not have adapter instance.
E/AndroidRuntime(23263):        at com.viewpagerindicator.TabPageIndicator.setViewPager(TabPageIndicator.java:205)
E/AndroidRuntime(23263):        at com.windmobile.rbt.android.category.CategoryActivity.onCreate(CategoryActivity.java:115)
E/AndroidRuntime(23263):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(23263):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
E/AndroidRuntime(23263):        ... 11 more

1 个答案:

答案 0 :(得分:1)

不要重置你的适配器。而不是那样,当异步任务完成时更新适配器中的数据源,并在适配器上调用notifyDataSetChanged()