ViewPager has not been bound [duplicate]
Asked Answered
S

1

-2

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

}

enter image description here

Suppletory answered 4/4, 2014 at 17:7 Comment(3)
a stacktrace and relevant code would helpDurazzo
@Durazzo I have added code snippet and exception Stack Trace.Kindly have a lookSuppletory
@Durazzo do you have any idea mate? i am going crazy fr ds... :(Suppletory
T
0

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.

Teece answered 4/4, 2014 at 18:15 Comment(4)
i know that and i have cross checked viewpager state at several stages ..but it wasnt the case.. thats why i m confused about this issue.Suppletory
Check last comment here - github.com/JakeWharton/Android-ViewPagerIndicator/issues/155Teece
i dont get it..i have tested the app on a physical device..still issue persists..anyways thx..:)Suppletory
Kanak.. My issue is different. If i call setContentView() from onPostExecute of AsynTask, then there is no issue, layout is rendered succesfully. What the problem is, if i call setContentView() in oncreate() method , then the above exception occurs. I would have continued with this approach but i need to show progress bars till the time AsyncTask finishes. I hope you have got my issue now.Suppletory

© 2022 - 2024 — McMap. All rights reserved.