问题描述
所以我工作的一个应用程序的朋友,它总是崩溃,无法到我的设备(一加手机)上运行
So I'm working on an app for a friend and it keeps crashing and is unable to run on my device (OnePlus One).
我一直在这里得到一个错误说不认为找到标识:
I keep getting an error here saying "No view found for id":
我有两个班;一类NavigationDrawer(这是我基本上是我的MainActivity):
I have two classes; a NavigationDrawer class (which is my essentially my MainActivity):
的Java
--- | XML
---
和一个名为StartingFragment类(我想是当抽屉被关闭/没有激活主视图)。
And a class named StartingFragment (which I want to be the main view when the drawer is closed/not active).
的Java
--- | XML
---
下面是code,其中有一个错误(从NavDrawer.java):
Here is the code in which there is an error (from 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);
}
我看了遍计算器,我看到其他的是如何得到这个错误,但我不能完全肯定为什么我不断收到此错误,或如何解决它。
I've looked all over StackOverflow and I see how other's are getting this error, but I'm not entirely sure of why I keep getting this error, or how to fix it.
我在这里看到了这个问题:
以及我看到的 R.id.starting_fragment 应是的孩子的在 R.layout.nav_drawer 的。
I looked at this question here:http://stackoverflow.com/a/8158916 and I see that R.id.starting_fragment should be a child of the R.layout.nav_drawer.
不过,我不知道在我的code调整。我应该删除我的按钮,然后有片段code在我的 nav_drawer.xml ?像这样的:
However, I don't know what to adjust in my code. Should I remove the Buttons I have and then have fragment code in my nav_drawer.xml? Such as this:
<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 在我的code?
推荐答案
在你给你的活动XML没有什么用id'starting_fragment因此没有更换,在XML的活动,需要有一个查看/布局/ fragmentContainer用id'starting_fragment。还你有点糊涂了,该标识在.replace()方法重新presents你要替换片段没有布局的事情
In the xml you gave for your activity there's no nothing with the id 'starting_fragment' therefore nothing to replace, in the xml for the activity there needs to be a view/layout/fragmentContainer with the id 'starting_fragment'. Also you're a bit confused, the Id in the .replace() method represents the thing you're going to replace not the layout of the fragment
简单的修复
在XML的活动创造的FrameLayout,给它的IDfragment_replace
in xml for the Activity create a frameLayout, give it the id 'fragment_replace'
更改.replace(R.id.starting_fragment,片段)以.replace(R.id.fragment_replace,片段)
Change .replace(R.id.starting_fragment,fragment) to .replace(R.id.fragment_replace,fragment)
这篇关于[否查看发现ID&QUOT; - Android的碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!