找不到用于ShooksFragment的ID的视图 [英] No view found for id for ShooksFragment
问题描述
我对所有这些片段内容都是陌生的,并且遇到了一个无法由StackOverflow的任何问题解决的错误.
I'm new to all this fragments stuff, and I get an error that couldn't be solved by any of the StackOverflow's questions.
我的问题是我得到
找不到片段ShooksFragment {1a77ec43#0 id = 0x7f0c0078的ID 0x7f0c0078(com.shook.android.shook:id/shooksLayout)的视图
No view found for id 0x7f0c0078 (com.shook.android.shook:id/shooksLayout) for fragment ShooksFragment{1a77ec43 #0 id=0x7f0c0078
尝试动态添加片段时.
我的代码:
MainActivity
MainActivity
...
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_friends) {
Toast.makeText(getApplicationContext(), "Find friends!", Toast.LENGTH_LONG).show();
} else if (id == R.id.nav_shooks) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
ShooksFragment fragment = new ShooksFragment();
transaction.add(R.id.shooksLayout, fragment);
transaction.commit();
} else if (id == R.id.nav_prizes) {
Toast.makeText(getApplicationContext(), "Get prizes!", Toast.LENGTH_LONG).show();
} else if (id == R.id.nav_share) {
Toast.makeText(getApplicationContext(), "Share your shooks!", Toast.LENGTH_LONG).show();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
...
ShooksFragment
ShooksFragment
public class ShooksFragment extends Fragment {
public ShooksFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_shooks, container, false);
}
// TODO: Rename and change types and number of parameters
public static ShooksFragment newInstance() {
ShooksFragment fragment = new ShooksFragment();
return fragment;
}
}
fragment_shooks.xml
fragment_shooks.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.shook.android.shook.ShooksFragment"
android:id="@+id/shooksLayout">
推荐答案
此行:
transaction.add(R.id.shooksLayout, fragment);
不正确,因为在片段
的布局中定义了 R.id.shooksLayout
.您在 add()
中指定的 id
应该是您在中指定的
进行活动.例如,对于片段,我通常在活动中保留一个空的 ViewGroup
的 id
setContentView()< FrameLayout>
,然后在两个片段 add()<中都提供此容器的
id
/code>和 replace()
调用.
is incorrect because R.id.shooksLayout
is defined in the layout of your fragment
. The id
you specify in add()
should be the id
of a ViewGroup
you specify in your setContentView()
fot the activity. For example, for my fragments I usually keep an empty <FrameLayout>
in my activity and then provide the id
of this container in both fragment add()
and replace()
calls.
这篇关于找不到用于ShooksFragment的ID的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!