gpt4 book ai didi

java - findFragmentByTag() 返回 null

转载 作者:行者123 更新时间:2023-11-30 03:19:52 25 4
gpt4 key购买 nike

我无法理解为什么 findFragmentByTag() 在我的代码中返回 null。我在我的 fragment onCreate() 上设置了 setRetainInstance(true) 并且我正在使用 getSupportFragmentManager().findFragmentByTag() 因为它是 SherlockFragmentActivity。

这是我的 Activity 代码:

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

}

这是我的 fragment 代码:

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

....
....
}

最佳答案

可能是因为您正在使用 ft.attach(frag); 重新附加 fragment 。查看文档:http://developer.android.com/reference/android/app/FragmentTransaction.html#attach(android.app.Fragment)

关于java - findFragmentByTag() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19349914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com