ViewPager尚未绑定

时间:2014-04-04 17:07:08

标签: android viewpagerindicator

当我在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);
}

}

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以查看TabPageIndicator.java库中的JakeWharton

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);
        }
    }
}

mViewPager为空时,抛出此错误。你需要交叉检查这个。