1
我不明白爲什麼findFragmentByTag()在我的代碼中返回null。 我對我的片段onCreate()有setRetainInstance(true),我使用getSupportFragmentManager()。findFragmentByTag(),因爲它的SherlockFragmentActivity。findFragmentByTag()返回null
這是我的活動代碼:
public class TedActivity extends SherlockFragmentActivity implements
TabListener, Constants {
private Tab tab;
private ActionBar actionBar;
private int currentTab;
MyTedFragment currentFrag;
private MyTedFragment previousFrag;
private static int count = 1;
private static final String TAG = TedActivity.class.getName();
private static final String FRAGMENT_PREFIX = "FRAGMENT";
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
currentTab = tab.getPosition();
currentFrag = (MyTedFragment) getSupportFragmentManager()
.findFragmentByTag(FRAGMENT_PREFIX + (currentTab + 1));
if (previousFrag != null) {
ft.hide(previousFrag);
}
if (currentFrag == null) {
Log.d(TAG, FRAGMENT_PREFIX + (currentTab + 1) + " = null");
} else {
Log.d(TAG, FRAGMENT_PREFIX + (currentTab + 1) + " != null");
ft.show(currentFrag);
}
previousFrag = currentFrag;
}
/**
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create first Tab
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
MyTedFragment frag = new MyTedFragment();
ft.add(android.R.id.content, frag, FRAGMENT_PREFIX + count);
ft.commit();
tab = actionBar.newTab().setTabListener(this);
tab.setText("Untitled" + count);
actionBar.addTab(tab);
count++;
if (savedInstanceState != null) {
actionBar.setSelectedNavigationItem(savedInstanceState.getInt(
"tab", 0));
}
}
public boolean onCreateOptionsMenu(Menu menu) {
addMenuItem(menu, MENU_ID_NEW, R.string.menu_new,
R.drawable.ic_action_new).setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS);
;
return true;
addMenuItem(menu, MENU_ID_OPEN, R.string.menu_open,
R.drawable.ic_action_open).setShowAsAction(
MenuItem.SHOW_AS_ACTION_IF_ROOM);
;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar()
.getSelectedNavigationIndex());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
currentFrag = (MyTedFragment) getSupportFragmentManager()
.findFragmentByTag(FRAGMENT_PREFIX + count);
if (currentFrag != null/* && !currentFrag.isDetached() */) {
currentFrag.mWarnedShouldQuit = false;
switch (item.getItemId()) {
case MENU_ID_NEW:
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
MyTedFragment frag = new MyTedFragment();
ft.add(android.R.id.content, frag, FRAGMENT_PREFIX + count);
ft.commit();
tab = actionBar.newTab().setTabListener(this);
tab.setText("Untitled" + count);
actionBar.addTab(tab);
actionBar.setSelectedNavigationItem(actionBar
.getNavigationItemCount() - 1);
count++;
// TODO mFragment.newContent();
return true;
case MENU_ID_OPEN:
currentFrag.openFile();
break;
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
這裏是我的代碼片段:
public class MyTedFragment extends SherlockFragment implements OnClickListener, KeyListener {
private EditText mEditor;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
// editor
mEditor = (EditText) getActivity().findViewById(R.id.editor);
mEditor.addTextChangedListener(this);
mEditor.updateFromSettings();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.layout_editor, null);
}
....
....
}
謝謝瓊。那種工作。現在問題是當我添加一個新的選項卡時,我仍然在選擇新選項卡時得到findFragmentByTag()返回null。 findFragmentByTag在創建新選項卡後在選項卡之間切換時返回非空值。可能是什麼原因造成的? – nmvictor
這可能是在onTabSelected上,您不要進行更改......給它一個鏡頭 –