当我在Async Task的onPostexecute方法完成后将适配器设置为viewpager时,我得到此异常并且应用程序强制关闭。 " ViewPager尚未绑定"
我正在使用针对TabPage指标的jakeWhartons Viewpager指标。
是否有解决方法?
代码段。
public class MyDetailedActivity extends FragmentActivity {
ViewPager pager;
PageIndicator mIndicator;
ProgressBar progressBar1;
ProgressBar progressBar2;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.ac_image_pager);
progressBar1 = (ProgressBar)findViewById(R.id.progressBarPager1);
progressBar2 = (ProgressBar)findViewById(R.id.progressBarPager2);
progressBar1.setVisibility(View.VISIBLE);
progressBar2.setVisibility(View.VISIBLE);
//Declare Pagers & Indicators
//Pager-Top
pager = (ViewPager) findViewById(R.id.pager);
pager.setClipToPadding(false);
pager.setPageMargin(12);
//Indicator of Pager-Top
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
new PastEventFullDetailDownloader(past_event_id).execute(null,null,null);
}
private class PastEventFullDetailDownloader extends AsyncTask<String,String, String>
{
protected String doInBackground(String... params) {
//Download data
}
protected void onPostExecute(String result) {
displayEvent(result);
}
}
void displayEvent(PastEventInfo event)
{
//hide the progressbars
progressBar1.setVisibility(View.GONE);
progressBar2.setVisibility(View.GONE);
//Set Up Adapter for First Pager ..Pass ImageUrsl Just Downloaded
pager.setAdapter(new ImagePagerAdapter(event.getEventImageUrsl()));
pager.setCurrentItem(pagerPosition);
mIndicator.setViewPager(pager);
}
}
答案 0 :(得分:0)
您可以查看TabPageIndicator.java
库中的JakeWharton
@Override
public void setCurrentItem(int item) {
if (mViewPager == null) {
throw new IllegalStateException("ViewPager has not been bound.");
}
mSelectedTabIndex = item;
mViewPager.setCurrentItem(item);
final int tabCount = mTabLayout.getChildCount();
for (int i = 0; i < tabCount; i++) {
final View child = mTabLayout.getChildAt(i);
final boolean isSelected = (i == item);
child.setSelected(isSelected);
if (isSelected) {
animateToTab(item);
}
}
}
当mViewPager
为空时,抛出此错误。你需要交叉检查这个。