findFragmentByTag()返回null

时间:2013-10-13 20:28:39

标签: java android android-fragments

我无法理解为什么findFragmentByTag()在我的代码中返回null。 我在我的片段onCreate()上有setRetainInstance(true),我使用的是getSupportFragmentManager()。findFragmentByTag(),因为它是一个SherlockFragmentActivity。

这是我的活动代码:

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

    ....
    ....
    }

1 个答案:

答案 0 :(得分:1)

可能是因为您正在使用ft.attach(frag);重新附加片段。查看文档:{​​{3}}