简体   繁体   中英

Android - Can not open fragment a second time

I'm opening a fragment and soon after closing it.

It opens and closes no problem the first time but the second time it tries to open it gives a NullPointerException .

To open the fragment with SomeFrag class that extends Fragment . To close it I call closeFrag() method.

public class SomeFrag extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootview = (ViewGroup) inflater.inflate(R.layout.somefrag, container, false); // line with error
        return mRootview;
    }

    public void closeFrag(){
        ((ViewGroup)mRootview.getParent()).removeView(mRootview);
    }
}

Why am I not able to open the fragment a second time?

例如,要删除片段,您应该使用事务

getSupportFragmentManager().beginTransaction().remove(fragmentInstance).commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM