屏幕锁定时应用程序崩溃

时间:2012-01-29 12:50:02

标签: android android-activity tabs android-fragments forceclose

我在logCat中收到错误“无法暂停活动”。我有一个包含3个页面的ViewPager,其内容是片段。这是一个NullPointerException报告。问题似乎出现在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

}
}

错误日志: http://dl.dropbox.com/u/33331786/Errors/log.txt

2 个答案:

答案 0 :(得分:0)

您的问题与FragmentManager :: saveFragmentBasicState中的错误有关。问题报告here中会报告错误(以及底部的解决方法)。

答案 1 :(得分:0)

我认为这个答案会有所帮助:

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

它帮助我解决了类似的问题。 请务必在所有碎片中包含此“修复”/“补丁”(不仅仅是在包含活动中)。

祝你好运!