【问题标题】:java.lang.IllegalArgumentException: No view found for id 0x7f0e0084 (com.example.dell.foodcourt:id/content) for fragment HomeFragmentjava.lang.IllegalArgumentException:未找到片段 HomeFragment 的 id 0x7f0e0084 (com.example.dell.foodcourt:id/content) 的视图
【发布时间】:2017-08-24 11:33:05
【问题描述】:

这是抽屉布局,我搜索了很多次以解决问题,但每次我都停在同一点......所以任何人都可以帮助我,我使用空白布局而不是预先设计来创建抽屉布局android Studio 中的抽屉布局

    <?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>

//这是Home Fragment 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);
}

}

//Home Fragment 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);
    }

}

【问题讨论】:

    标签: java android eclipse android-layout android-fragments


    【解决方案1】:

    在您的 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,你在 10 分钟内解决了我 3 天的问题...向天才致敬...
    • 不客气 :) 请考虑通过单击复选标记接受答案。这向社区表明您已找到解决方案。没有义务这样做。
    • Cochi 再问一个问题,当我点击菜单项中的项目时,它正在移动到片段,但没有显示与家庭或个人资料相关的片段信息
    • 抱歉回复晚了,但我得到了预期的结果...谢谢。 .. :)
    猜你喜欢
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多