【发布时间】:2014-12-29 18:06:14
【问题描述】:
所以我正在为朋友开发一个应用程序,但它一直在崩溃并且无法在我的设备(OnePlus One)上运行。
我在这里不断收到一条错误消息,提示“找不到 id 的视图”:
我有两节课; 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?
【问题讨论】:
标签: java android xml android-fragments android-identifiers