简体   繁体   中英

Android Fragment Error: No View Found For ID

The error in logcat: E/FragmentManager(7158): No view found for id 0x7f05000d (com.mybiz.mygame:id/fragment_container) for fragment MainMenuFragment{4052d780 #0 id=0x7f05000d}

Ok here is how the app is set up:

Two vars are created:

private static View oGoogleGamesView ;
private static RelativeLayout oViewGroup ;

Then in onCreate:

oViewGroup = new RelativeLayout ( this ) ;
setContentView ( oViewGroup ) ;

LayoutInflater inflater = (LayoutInflater)this.getSystemService ( Context.LAYOUT_INFLATER_SERVICE ) ;       
oGoogleGamesView        = inflater.inflate ( R.layout.ggmain, oViewGroup, false ) ;

And then I try to show the fragment:

// create fragments
mMainMenuFragment = new MainMenuFragment();

// listen to fragment events
mMainMenuFragment.setListener(this);

// add initial fragment (welcome fragment)
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
        mMainMenuFragment).commit();

And here it crashes with the error at top. Here is the xml file:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</FrameLayout>

What can I do to show the fragment without crashing?

Try this in onCreate method setContentView(R.layout.yourLayout); where yourLayout is xml file containing frameLayout that you have put in your question.

You set your app view to oViewGroup:

setContentView ( oViewGroup ) ;

Then you inflate another view and leave it dangling:

oGoogleGamesView        = inflater.inflate ( R.layout.ggmain, oViewGroup, false ) ;

To actually have oGoogleGamesView as a layout in your app, you have to add it to the current view, only then can you reference the IDs contained in it:

oViewGroup.addView(oGoogleGamesView, LayoutParams);

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