1

我无法framgent通过检索findFragmentByTag。请帮忙!

请参阅下面的代码。当我检索片段时,我为 NetEaseContent 定义了一个标签,它丢失并返回null

 public class MainActivity extends SlidingFragmentActivity {

    private static final String MENU_TAG = "menuTag";
    private Screen screen;
    private SlidingMenu mSlideMenu;
    private final static String CONTENTTAG = "contentTag";
    private static final String TAG = "MainActivity";
    private NetEaseContent mContent;
    private Fragment mMenu;
    private Bundle mBundle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.content_frame);
        setBehindContentView(R.layout.menu_frame);
        Log.e(TAG, "onCreate");
        screen = new Screen(this);
        Log.e(TAG, "savedInstanceState null");
        mSlideMenu = getSlidingMenu();
        mSlideMenu.setEnabled(true);
        mSlideMenu.setShadowWidthRes(R.dimen.shadow_width);
        mSlideMenu.setShadowDrawable(R.drawable.shadow);
        mSlideMenu.setFadeDegree(0.35f);
        mSlideMenu
                .setBehindOffset((int) ((float) screen.getWidth() * 2.5 / (float) 4));
        mSlideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);


        if (savedInstanceState == null) {
            mContent = new NetEaseContent(mSlideMenu);
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.content_frame, mContent, CONTENTTAG).commit();
            mMenu = new NetEaseMenuFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.menu_frame, mMenu, MENU_TAG).commit();
        } else {
            Log.e(TAG, "savedInstanceState not null!!!");
            mContent = (NetEaseContent)getSupportFragmentManager()
                    .findFragmentByTag(CONTENTTAG);
            if (mContent != null) {
                mContent.setSlidingMenu(mSlideMenu);
            } else {
                Log.e(TAG, "mContent  null!!!");  //here the mContent filed is null 
            }
            mMenu = getSupportFragmentManager().findFragmentByTag(MENU_TAG);

        }
    }
}
4

1 回答 1

0

您是否在片段 onCreate 方法中使用了 setRetainInstance(true)?如果没有,您的片段会在活动生命周期中被销毁

于 2013-06-06T09:17:50.750 回答