Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android FindFragmentByTag返回null_Android_Fragment - Fatal编程技术网

Android FindFragmentByTag返回null

Android FindFragmentByTag返回null,android,fragment,Android,Fragment,我无法通过findFragmentByTag检索framgent。请帮忙 请参阅下面的代码。检索片段时,我已为NetEaseContent定义了一个标记,该标记缺失并返回null: 在fragment onCreate方法中是否使用了setRetainInstancetrue?如果没有,您的片段将在活动生命周期中销毁谢谢。我发现了导致零点异常的问题。我在片段中添加了额外的构造函数,所以android无法找到片段。我还发现,如果我添加了由调用findFragmentbyTag的活动调用的额外方法,

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

请参阅下面的代码。检索片段时,我已为NetEaseContent定义了一个标记,该标记缺失并返回null:


在fragment onCreate方法中是否使用了setRetainInstancetrue?如果没有,您的片段将在活动生命周期中销毁

谢谢。我发现了导致零点异常的问题。我在片段中添加了额外的构造函数,所以android无法找到片段。我还发现,如果我添加了由调用findFragmentbyTag的活动调用的额外方法,该方法也会返回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);

        }
    }
}