1

I get an error "Unable to pause activity" in the logCat. I have a ViewPager with 3 pages which contents are fragments. Theres a NullPointerException report. Problem seems to be in the MainActivity code. Heres my MainActivity:

public class MainActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
private PagerAdapter mPagerAdapter;

private class TabInfo {
     private String tag;
     private Class<?> clss;
     private Bundle args;
     private Fragment fragment;
     TabInfo(String tag, Class<?> clazz, Bundle args) {
         this.tag = tag;
         this.clss = clazz;
         this.args = args;
     }
}
class TabFactory implements TabContentFactory {

    private final Context mContext;

    public TabFactory(Context context) {
        mContext = context;
    }
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.initialiseTabHost(savedInstanceState);
    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }
    this.intialiseViewPager();
}
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag());
    super.onSaveInstanceState(outState);
}
private void intialiseViewPager() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName()));
    fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName()));
    fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName()));
    this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);

    this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
    this.mViewPager.setAdapter(this.mPagerAdapter);
    this.mViewPager.setOnPageChangeListener(this);
}
private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("easy").setIndicator(createTabView(this, "EASY")), ( tabInfo = new TabInfo("easy", Tab1Fragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("medium").setIndicator(createTabView(this, "MEDIUM")), ( tabInfo = new TabInfo("medium", Tab2Fragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("intense").setIndicator(createTabView(this, "INTENSE")), ( tabInfo = new TabInfo("intense", Tab3Fragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    mTabHost.setOnTabChangedListener(this);
}
private static View createTabView(final Context context, final String text) {
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    TextView tv = (TextView) view.findViewById(R.id.tabText);
    tv.setText(text);
    return view;
}

private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {

    tabSpec.setContent(activity.new TabFactory(activity));
    tabHost.addTab(tabSpec);
}

public void onTabChanged(String tag) {

    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);
}

@Override
public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
    // TODO Auto-generated method stub
    this.mTabHost.setCurrentTab(position);
}

@Override
public void onPageScrollStateChanged(int state) {
    // TODO Auto-generated method stub

}
}

Error Log: http://dl.dropbox.com/u/33331786/Errors/log.txt

borislemke
  • 8,446
  • 5
  • 41
  • 54
  • You have an onSaveInstanceState, but where is the corresponding onRestoreInstanceState? – Joachim Isaksson Jan 29 '12 at 12:54
  • @JoachimIsaksson Im sorry, where do I have to call onRestoreInstanceState ? – borislemke Jan 29 '12 at 13:04
  • onSaveInstanceState is called when your activity needs to save its state, there you're saving a "tab" setting. onRestoreInstanceState is called when your activity needs to restore its saved state, it should do the reverse, read "tab" and set it as the current tab tag. Anything you're not saving in onSave/loading in onRestore is probably null when your activity saves/restores. Which could happen for example when your screen is locked/unlocked. – Joachim Isaksson Jan 29 '12 at 13:10
  • @JoachimIsaksson I tried adding public void onRestoreInstanceState(Bundle b){ if (b != null){ mTabHost.getCurrentTabTag() = b.getLong("tab"); } } Nothing changed – borislemke Jan 29 '12 at 13:14
  • Do you have a stacktrace to add to the question? – Joachim Isaksson Jan 29 '12 at 13:17
  • @JoachimIsaksson I posted a link – borislemke Jan 29 '12 at 13:27
  • Did you use a debugger to see, which reference is actually pointing to `null` in `onSaveInstanceState`? This might get you one step further. – henrik Jan 29 '12 at 16:06

2 Answers2

0

Your problem is related to a bug in FragmentManager::saveFragmentBasicState. The bug (and a workaround at the bottom) are reported in an issue report here.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

I think this answer will be helpful:

https://stackoverflow.com/a/9446326/1205281

It helped me with similiar issue. Be sure to include this "fix"/"patch" to all of your Fragments (not only in the containing activity).

Good luck!

Community
  • 1
  • 1
David Avikasis
  • 516
  • 1
  • 5
  • 16