java.lang.IllegalArgumentException:没有找到ID为0x7f0e0084(com.example.dell.foodcourt:id/content)的片段HomeFragment
问题描述:
这是抽屉布局,我搜索了很多次来解决问题,但每次我我停在同一点...所以任何人可以请帮助我,我用的,而不是预先设计的抽屉布局Android Studio中的空白布局创建抽屉布局java.lang.IllegalArgumentException:没有找到ID为0x7f0e0084(com.example.dell.foodcourt:id/content)的片段HomeFragment
<?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>
//这是为家庭片段XML布局布局
<RelativeLayout 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:id="@+id/homeFragment"
tools:context="com.example.dell.foodcourt.HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
android:text="@string/hello_blank_fragment" />
</RelativeLayout>
//抽屉布局Java类
package com.example.dell.foodcourt;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import static android.app.PendingIntent.getActivity;
/**
* Created by DELL on 19-08-2017.
*/
public class Userhome extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle toggle;
private NavigationView navigationview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_homepage);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
toggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
navigationview= (NavigationView) findViewById(R.id.navigationmenu);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
setupDrawerContent(navigationview);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
private void setupDrawerContent(NavigationView navigationView){
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectDraweritem(item);
return true;
}
});
}
//我在这里主要面临的问题,到了我的知识,所以任何人可以帮我解决这个问题
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>
希望这有助于
真的很感谢你们cochi,你只是解决了我的3天问题,在10分钟...帽子天才... – satish
欢迎您:)请考虑通过点击复选标记接受答案。这表明你已经找到了解决方案。没有义务这样做。 – Cochi
Cochi还有一个问题,当我点击菜单项中的项目时,它正在移动到片段,但没有显示与此相关的家庭或个人资料相关的片段信息 – satish