java.lang.IllegalArgumentException:未找到片段HomeFragment的id 0x7f0e0084(com.example.dell.foodcourt:id/content)的视图
这是抽屉布局,我搜索了很多次来解决这个问题,但每次我都停在同一个点上…所以有人能帮我吗?我在android Studio中使用空白布局而不是预先设计的抽屉布局来创建抽屉布局java.lang.IllegalArgumentException:未找到片段HomeFragment的id 0x7f0e0084(com.example.dell.foodcourt:id/content)的视图,java,android,eclipse,android-layout,android-fragments,Java,Android,Eclipse,Android Layout,Android Fragments,这是抽屉布局,我搜索了很多次来解决这个问题,但每次我都停在同一个点上…所以有人能帮我吗?我在android Studio中使用空白布局而不是预先设计的抽屉布局来创建抽屉布局 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerlayout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationmenu"
android:layout_height="match_parent"
android:layout_width="wrap_content"
app:menu="@menu/menudetails"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
//据我所知,我主要是在这里面对这个问题,有人能帮我解决这个问题吗
public void selectDraweritem(MenuItem menuItem) {
Fragment fragment=null;
switch (menuItem.getItemId()){
case R.id.homeFragment:
fragment=new HomeFragment();
break;
case R.id.navigation_Profile:
fragment=new Profile_Fragment();
break;
default:
fragment=new HomeFragment();
break;
}
if (fragment!=null){
FragmentManager fragmentManager=getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content,fragment).commit();
}
DrawerLayout drawerLayout= (DrawerLayout) findViewById(R.id.drawerlayout);
drawerLayout.closeDrawer(GravityCompat.START);
}
public boolean onOptionsItemSelected(MenuItem item){
if(toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
}
//主片段java类在这里
package com.example.dell.foodcourt;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.zip.Inflater;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
inflater.inflate(R.layout.fragment_home,container,false);
if(savedInstanceState==null){
getFragmentManager().beginTransaction().replace(R.id.content,new HomeFragment()).commit();
}
return super.onCreateView(inflater,container,savedInstanceState);
}
}
在HomeFragment中,应在onCreateView方法生命周期中返回要充气的视图,例如:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home,container,false);
// This part should be in the activity who handle the fragment
/*if(savedInstanceState==null){
getFragmentManager().beginTransaction().replace(R.id.content,new HomeFragment()).commit();
}*/
return rootView;
}
此外,为了显示片段,您可以在活动布局中添加一个FrameLayout作为片段容器:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerlayout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</LinearLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/navigationmenu"
android:layout_height="match_parent"
android:layout_width="wrap_content"
app:menu="@menu/menudetails"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
希望能有所帮助真的谢谢你,科奇,你刚刚在10分钟内解决了我3天的问题。。。取消天才:欢迎你:请考虑通过点击复选标记接受答案。这向社区表明您已经找到了解决方案。没有义务这么做。Cochi还有一个问题,当我点击菜单项中的项目时,它正在移动到片段,但没有显示与主页或个人资料相关的片段信息,就像那样对不起,回复太晚了,但我得到了预期的结果。。。谢谢你……:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home,container,false);
// This part should be in the activity who handle the fragment
/*if(savedInstanceState==null){
getFragmentManager().beginTransaction().replace(R.id.content,new HomeFragment()).commit();
}*/
return rootView;
}
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawerlayout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"/>
</LinearLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/navigationmenu"
android:layout_height="match_parent"
android:layout_width="wrap_content"
app:menu="@menu/menudetails"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>