【问题标题】:Android Fragment Error: No View Found For IDAndroid 片段错误:找不到 ID 的视图
【发布时间】:2013-10-11 04:52:01
【问题描述】:

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

好的,这是应用程序的设置方式:

创建了两个变量:

private static View oGoogleGamesView ;
private static RelativeLayout oViewGroup ;

然后在 onCreate:

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

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

然后我尝试展示片段:

// 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();

在这里它崩溃并在顶部显示错误。这是xml文件:

<?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>

如何在不崩溃的情况下显示片段?

【问题讨论】:

    标签: android android-layout android-fragments android-view shiva3d


    【解决方案1】:

    您将应用视图设置为 oViewGroup:

    setContentView ( oViewGroup ) ;
    

    然后你膨胀另一个视图并让它悬空:

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

    要真正将oGoogleGamesView 作为应用中的布局,您必须将其添加到当前视图中,然后才能引用其中包含的 ID:

    oViewGroup.addView(oGoogleGamesView, LayoutParams);
    

    【讨论】:

      【解决方案2】:

      试试这个 在 onCreate 方法中 setContentView(R.layout.yourLayout); 其中 yourLayout 是包含您在问题中提出的 frameLayout 的 xml 文件。

      【讨论】:

      • 感谢您的回复,但是这样做会破坏我的应用程序,我需要将我的 ContentView 设置为 oViewGroup。
      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      相关资源
      最近更新 更多