繁体   English   中英

“找不到ID的视图”-Android片段

[英]“No View Found For ID” - Android Fragment

因此,我正在为一个朋友开发一个应用程序,该应用程序不断崩溃,无法在我的设备(OnePlus One)上运行。

我在这里不断收到错误消息,说“找不到ID的视图”: Logcat错误视图

我有两节课; NavigationDrawer类(本质上是我的MainActivity):

Java --- http://pastebin.com/4BiYvxiS | XML --- http://pastebin.com/Fnat83uf

还有一个名为StartingFragment的类(当抽屉关闭/未激活时,我想成为主视图)。

Java --- http://pastebin.com/HNeHzx1h | XML-- http://pastebin.com/2viTEWR8

以下是其中存在错误的代码(来自NavDrawer.java):

        /** Swaps fragments in the main content view */
        /** Starts an Activity when item is clicked */
        private void selectItem(int position) {
                // Create a new fragment and specify the tea type
                // to show based on position
                Fragment fragment = new StartingFragment();
                Bundle args = new Bundle();
                args.putInt(StartingFragment.TEA_TYPE_POS, position);
                fragment.setArguments(args);

                // Insert the fragment by replacing any existing fragment
                FragmentManager fragManager = getFragmentManager();
                fragManager.beginTransaction().replace(R.id.starting_fragment, fragment)
                                .commit();

                // Highlight the selected item, update the title, and close the drawer
                mDrawerList.setItemChecked(position, true);
                setTitle(navDrawerTitles[position]);
                navDrawerLayout.closeDrawer(mDrawerList);

        }

我到处都是StackOverflow,看看其他人是怎么得到这个错误的,但是我不完全确定为什么我继续收到这个错误,或者如何解决它。

我在这里查看了这个问题: https : //stackoverflow.com/a/8158916 ,我看到R.id.starting_fragment应该是R.layout.nav_drawer 的子级

但是,我不知道要在代码中进行哪些调整。 我应该删除已有的按钮,然后在nav_drawer.xml中添加片段代码吗? 如:

<fragment android:name="com.fv4.app.StartingFragment"
              android:id="@+id/starting_fragment"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

还是真的...我应该在我的代码中用R.id.starting_fragment替换什么?

在您为活动提供的xml中,没有任何ID为'starting_fragment'的内容,因此也无需替换。在该活动的xml中,需要有一个ID为'starting_fragment'的view / layout / fragmentContainer。 另外,您还有些困惑,.replace()方法中的ID表示要替换的内容,而不是片段的布局

简单修复

在xml中为Activity创建一个frameLayout,为其指定id'fragment_replace'

将.replace(R.id.starting_fragment,fragment)更改为.replace(R.id.fragment_replace,fragment)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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