私のアプリには 2 つのタブと 4 つのフラグメントがあります。
フラグメント 1 と 2 はフラグメント 3 と 4 に置き換えられます: フラグメント 1 -> フラグメント 3 | フラグメント 2 -> フラグメント 4
このフラグメントの交換には、FragmentPagerAdapter を使用しています。アプリが LogCat にエラーを表示しないため、フラグメントの交換が行われます。クラッシュまたは終了します。しかし、アプリの画面が真っ白に!空白の画面だけが表示されます。理解できません。
誰でもエラーを理解するのを手伝ってもらえますか?
ページャー アダプターのコードに従います。
<!-- language: lang-java -->
public class PagerAdapter extends FragmentPagerAdapter{
private final Context mContext;
private final FragmentManager mFragmentManager;
private final TabHost mTabHost;
private final ViewPager mViewPager;
private List<String> mTabTags = new ArrayList<String>();
private HashMap<Integer,Stack<String>> mFragmentsBack = new HashMap<Integer,Stack<String>>();
private HashMap<String, Info> mFragments = new HashMap<String, Info>();
private boolean mBackFragent = true;
private int mTabPosition = 0;
public PagerAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
super(activity.getSupportFragmentManager());
this.mContext = activity;
this.mFragmentManager = activity.getSupportFragmentManager();
this.mTabHost = tabHost;
this.mTabHost.setOnTabChangedListener(tabChangeListener);
this.mViewPager = pager;
this.mViewPager.setOnPageChangeListener(viewPageChangeListener);
this.mViewPager.setAdapter(this);
this.mViewPager.setPageMargin(8);
this.mViewPager.setCurrentItem(0);
this.mTabTags.clear();
this.mFragments.clear();
this.mFragmentsBack.clear();
this.mBackFragent = true;
this.mTabPosition = 0;
}
@Override
public Fragment getItem(int position) {
String tag = mTabTags.get(position);
Info info = mFragments.get(tag);
Fragment fragment = info.getFragment();
if(fragment == null){
Log.d("getItem","null: "+info.getClss().getName());
fragment = Fragment.instantiate(mContext, info.getClss().getName(), info.getArgs());
info.setFragment(fragment);
mFragments.put(tag,info);
}else{
Log.d("getItem","instatiated "+info.getClss().getName());
}
return fragment;
}
@Override
public int getItemPosition(Object object){
ArrayList<Class<?>> positionNoneHack = new ArrayList<Class<?>>();
for(Info info: mFragments.values())
{
positionNoneHack.add(info.getClss());
}
if(positionNoneHack.contains(object.getClass()))
{
return POSITION_UNCHANGED;
}
return POSITION_NONE;
}
@Override
public int getCount()
{
return mTabTags.size();
}
public void addTab(TabSpec tabSpec)
{
try
{
this.mTabHost.setup();
tabSpec.setContent(new TabContent(this.mContext));
this.mTabHost.addTab(tabSpec);
this.mTabTags.add(tabSpec.getTag());
this.mFragmentsBack.put(this.mTabPosition, new Stack<String>());
this.mTabPosition++;
this.notifyDataSetChanged();
}
catch(Exception ex){
ex.printStackTrace();}
}
public void addFragment(String tag, Class<?> clss, Bundle args){
Info info = new Info(tag, clss, args);
this.mFragments.put(tag,info);
}
public void replaceFragment(String tagOld, String tagNew, Bundle argsNew)
{
try
{
Log.d("replaceFragment","old: "+tagOld+", new: "+tagNew);
Fragment oldFragment = mFragments.get(tagOld).getFragment();
if(oldFragment == null)
{
return;
}
int oldPosition = mTabTags.indexOf(tagOld);
mTabTags.set(oldPosition, tagNew);
if(this.mBackFragent)
{
int location = mViewPager.getCurrentItem();
Stack<String> stackFragmentsBack = this.mFragmentsBack.get(location);
stackFragmentsBack.add(tagOld);
this.mFragmentsBack.put(location, stackFragmentsBack);
}
else
{
this.mBackFragent = true;
}
Fragment newFragment = this.getItem(oldPosition);
if(argsNew != null)
{
newFragment.setArguments(argsNew);
}
this.startUpdate(mViewPager);
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//ft.hide(oldFragment);
ft.remove(oldFragment);
ft.add(mViewPager.getId(), newFragment);
ft.show(newFragment);
ft.commit();
mFragmentManager.executePendingTransactions();
this.notifyDataSetChanged();
this.finishUpdate(mViewPager);
}
catch(Exception ex){
ex.printStackTrace();}
}
public boolean back()
{
try
{
int location = mViewPager.getCurrentItem();
Stack<String> stackFragmentsBack = mFragmentsBack.get(location);
String tagBack = stackFragmentsBack.pop();
if(tagBack == null)
{
return false;
}
String tagCurrent = mTabTags.get(location);
this.mBackFragent = false;
this.mFragmentsBack.put(location, stackFragmentsBack);
this.replaceFragment(tagCurrent, tagBack, null);
return true;
}
catch(Exception ex){
ex.printStackTrace();
return false; }
}
private final TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
int pos = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(pos);
}};
private final ViewPager.OnPageChangeListener viewPageChangeListener = new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int position) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(position);//original code just with this line
widget.setDescendantFocusability(oldFocusability);
}
};
}