2012-11-20 43 views
2

我对ViewPager有问题。当活动开始我运行的AsyncTask从服务器下载数据,我叫pager.setAdapter(currentPager)currentPager是带标签的空片段为ViewPageIndicator.TabPageIndicatorViewPager不刷新第二次调用后查看setAdapter()

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

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

时的AsyncTask完成下载数据我打电话pager.setAdapter(currentPager);和第2页是空的,其他页面都OK。如何首先刷新到具有适当数据的页面? 我不能调用的setContentView没有pager.setAdapter(currentPager);,因为我有一个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 
+2

你有答案吗?我面临的问题与您的问题类似。 – hemu

+0

你有答案吗? – Rudi

回答

1

不要重置适配器。相反,在异步任务完成时更新适配器中的数据源,并在适配器上调用notifyDataSetChanged()

+0

我添加currentPager.notifyDataSetChanged();在onSuccess()中的AsyncTask中,我也尝试了pager.getAdapter()。notifyDataSetChanged();但它不起作用。我做错了吗? – plancys

+0

你如何将数据插入适配器? –

+0

我在Activity中为适配器创建变量,首先它是空的(不是null,但它有空的碎片),我调用setContentView,接下来的AsyncTask更改这个变量,并且我在适配器上调用notifyDataSetChanged()。 – plancys