简体   繁体   中英

No view found for id for fragment

I need a help. I got the crash on Crashlytics:

Fatal Exception: java.lang.IllegalArgumentException: No view found for id 0x7f0f016c (com.my.my:id/fragment_root_layout) for fragment a{2e8d61e1 #4 id=0x7f0f016c fragment_tag}

I use fragment into fragment. In OnCreate method of parent fragment I add child fragment with help FragmentManager. I have an idea that problem is child fragment adding in OnCreate method and need do this in OnCreateView. But I'm not sure. The thing is I can't reproduce this crash, but I get this crash on Crashlytic. All views on parent fragment layount have an id. I can reproduce this crash only if I inflate incorrect layout in OnCreateView method, but this can't happen in live build. Please give me an advice.

EDIT: Code of adding child fragment

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.verbose(TAG, "displayOverlayFragment: attempt to add a fragment using FragmentTransaction");
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        OverlayFragment overlayFragment = new OverlayFragment();
        fragmentTransaction.add(R.id.fragment_root_layout, overlayFragment, FRAGMENT_TAG);
        fragmentTransaction.commit();
    }

Code of onCreateView:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResId(), container, false);
        mRecyclerView = initRecyclerView(view);

        mProgressBar = view.findViewById(R.id.progressBar);
        mProgressBarLabel = (TextView) view.findViewById(R.id.progressBarLabel);
        }

        return view;
}
@Override
    protected int getLayoutResId() {
        return R.layout.fragment_favorites;
    }

Maybe you have to declare something in proguard-rules.pro in your release build ? Did you check if its happens in release build only ? I think your debug version ignores proguard and it's works fine, but if you'll check your release version you will have chance to reproduce your crash. Maybe some of Fragment child components should be declared in proguard.

For Fragments in android you need to code them in the onStart() method. And use the onCreateView() to return the layout you want to ue for that particular fragment

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