问题
When i set the adapters to viewpager after the onPostexecute method of Async Task has finished, i get this Exception and app Force closes. "ViewPager has not been bound"
I am using jakeWhartons Viewpager indicator for TabPage Indicators.
Is there a fix to it?
code snippet.
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);
}
}
回答1:
You ca check the TabPageIndicator.java
from JakeWharton
library
https://github.com/JakeWharton/Android-ViewPagerIndicator/blob/master/library/src/com/viewpagerindicator/TabPageIndicator.java
@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);
}
}
}
This error is thrown, when mViewPager
is null. You need to cross check this.
来源:https://stackoverflow.com/questions/22868885/viewpager-has-not-been-bound