首页 > 解决方案 > IllegalArgument 异常 未找到视图

问题描述

这是我得到的错误:

java.lang.IllegalArgumentException: No view found for id 0x7f09003d (com.company.app:id/container) for fragment friendFragment{118ad082 #0 id=0x7f09003d}

我做了一些调试,发现错误来自这一行:

transaction.replace(R.id.container, friendFragment).commit();

在我的布局 xml 文件中,我创建了一个名为的片段R.id.container

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" />

OnCreate 代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //TODO Add Fragment to activity using getSupportFragmentManager() method

    FriendFragment friendFragment= new FriendFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.container, friendFragment).commit();                  //This line

与我的片段类:

   public static class FriendFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.pref_frag_main, "friend_fragment");

    }

}

我很想帮助我解决这个问题,因为我不知道为什么会这样?

任何帮助、答案或建议都会很棒!

更新: 感谢@MikeM,我解决了这个问题。在评论中查看下面的解决方案!

标签: javaandroidandroid-fragmentsexception

解决方案


推荐阅读