java.lang.IllegalArgumentException:找不到片段HomeFragment

时间:2017-08-24 11:33:05

标签: 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: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 class is here

 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);
    }

}

1 个答案:

答案 0 :(得分:0)

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

希望这有帮助