Android 片段未显示在MainActivity页面中

Android 片段未显示在MainActivity页面中,android,android-fragments,Android,Android Fragments,大家好,我使用左边的导航栏,在导航栏中使用fragment进行活动。但问题是,一旦我点击活动,片段就无法显示。我不确定是什么问题 主要活动 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.nav_layout); Toolbar toolbar = (Toolbar) findViewById(

大家好,我使用左边的导航栏,在导航栏中使用fragment进行活动。但问题是,一旦我点击活动,片段就无法显示。我不确定是什么问题

主要活动

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nav_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    initCollapsingToolbar();

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    informationList = new ArrayList<>();
    adapter = new AlbumsAdapter(this, informationList);

    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 
2);
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new GridSpacingItemDecoration(2,  
dpToPx(10), true));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);


    prepareAlbums();

    //Cover Pic

    try {
        Glide.with(this).load(R.drawable.project1_cover).into((ImageView) 
findViewById(R.id.backdrop));
    } catch (Exception e) {
        e.printStackTrace();
    }


    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, 
R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) 
findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    setupDrawerContent(navigationView);



}
public void selectedItemDrawer(MenuItem menuItem){
    Fragment myFragment = null;
    Class fragment = null;

    switch(menuItem.getItemId()){

        case R.id.checkIn:
            Toast.makeText(getApplicationContext(), "Check-In", 
Toast.LENGTH_SHORT).show();
            break;

        case R.id.checkOut:
            Toast.makeText(getApplicationContext(), "Check-Out", 
Toast.LENGTH_SHORT).show();
            break;

        case R.id.applyOff:
            break;

        case R.id.reportBug:
            break;

        case R.id.manageProfile:
            fragment = fragment_manageProfile.class;
            break;

        case R.id.logout:
            break;

        default:
            break;
    }
    try{
        myFragment = (Fragment)fragment.newInstance();
    }catch(Exception e){
        e.printStackTrace();
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frameLayout, 
myFragment).addToBackStack(null).commit();
    menuItem.setChecked(true);
    drawer.closeDrawers();
}

活动布局文件中的FrameLayout是正常的,因为您希望在同一屏幕上显示片段。关于片段显示。您需要有一个自定义片段类来显示它的视图并支持它的业务逻辑

public void selectedItemDrawer(MenuItem menuItem){
    Fragment myFragment = null;
    Class fragment = null;

    switch(menuItem.getItemId()){

        case R.id.checkIn:
            Toast.makeText(MainActivity.this, "Check-In", 
Toast.LENGTH_SHORT).show();
            break;

        case R.id.checkOut:
            Toast.makeText(MainActivity.this, "Check-Out", 
Toast.LENGTH_SHORT).show();
            break;

        case R.id.applyOff:
            break;

        case R.id.reportBug:
            break;

        case R.id.manageProfile:
            showMyFragment();
            break;

        case R.id.logout:
            break;

        default:
            break;
    }

}

public void showMyFragment(){
    Fragment menuFragment = new MyCustomFragment();
       FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frameLayout, menuFragment).commit();
}
在这里,您必须声明自定义片段类:

public class MyCustomFragment extends Fragment {

//this text view is got from the xml below 
private TextView tvTest;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
              return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        bindView(view);

        //start your logic here
    }



    private void bindView(View view){
     tvTest = view.findViewById(R.id.tv_fragment_test);

     Toast.makeText(getContext(),tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
    }
  }
my_fragment_layout.xml

// could be any kind of layout
  // for example

  <?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/tv_fragment_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I'm showing on a fragment"
        android:src="@drawable/app_img_small" />

</LinearLayout>
//可以是任何类型的布局
//比如说

堆栈跟踪打印什么?也许,它无法创建新的片段。“碎片是什么?”萨尔曼说,没有错误。但这只是一个片段。哎呀,忘了发布我的片段类了,我将在这里发布我的片段类。sry。我仍然对这个片段感到困惑。功能是什么?你能用简单的方式解释吗?谢谢你。@davidam肯定:D.创建该自定义片段是为了替换活动中添加的框架布局。基本上,自定义片段表示在片段启动时创建的视图。您希望在单击导航抽屉时使用一个片段,因此,这样做时将创建一个片段,但代码中没有对任何现有片段的引用,您只创建了对默认片段类的引用,该类不支持对它的查看。换句话说,片段有自己的布局,您可以将其视为“另一个屏幕”。谢谢,但是我如何将我的片段活动链接到自定义片段?请查看上面的示例
public void showMyFragment(){fragment menuffragment=new MyCustomFragment();FragmentManager FragmentManager=getSupportFragmentManager();
在这里,我正在创建自定义片段的一个新实例,
fragment menuframent=new MyCustomFragment();
然后,我用表示自定义片段实例的menuframent替换活动中的FrameLayout。
<FrameLayout 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"
tools:context="info.androidhive.kopilim.fragment_manageProfile">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="35dp"
            android:textSize="22dp"
            android:textColor="#10019f"
            android:textStyle="bold"
            android:text="@string/manageProfile"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/empEmail"
            android:text="@string/email"
            android:layout_margin="10dp"
            android:textSize="18dp"
            android:gravity="center_vertical"
            android:textColor="@color/colorAccent"
            />

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/colorAccent"
            android:layout_margin="5dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/empID"
                android:hint="@string/profileID"
                android:textColor="@color/colorAccent"
                />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/colorAccent"
            android:layout_margin="5dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/empName"
                android:hint="@string/profileName"
                android:textColor="@color/colorAccent"
                />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/colorAccent"
            android:layout_margin="5dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/empIC"
                android:hint="@string/profileIC"
                android:textColor="@color/colorAccent"
                />
        </android.support.design.widget.TextInputLayout>

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/radioGroup"
            android:layout_margin="5dp"
            android:orientation="horizontal"
            android:weightSum="1"
            >

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/male"
                android:id="@+id/maleRadio"
                android:textColor="@color/colorAccent"
                android:layout_weight="0.3"
                android:checked="true" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/female"
                android:id="@+id/femaleRadio"
                android:textColor="@color/colorAccent"
                android:layout_weight="0.3"/>

        </RadioGroup>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/colorAccent"
            android:layout_margin="5dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/empPh"
                android:hint="@string/profileHp"
                android:textColor="@color/colorAccent"
                />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/colorAccent"
            android:layout_margin="5dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/empAddress"
                android:hint="@string/profileAddress"
                android:textColor="@color/colorAccent"
                />
        </android.support.design.widget.TextInputLayout>



        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:id="@+id/profileBtn"
            android:text="@string/updateProfileBtn"
            android:background="@color/buttonLogin"/>

    </LinearLayout>

</ScrollView>

</FrameLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_manage_profile, container, 
false);
....
....
//my activity
return v;
}
public void selectedItemDrawer(MenuItem menuItem){
    Fragment myFragment = null;
    Class fragment = null;

    switch(menuItem.getItemId()){

        case R.id.checkIn:
            Toast.makeText(MainActivity.this, "Check-In", 
Toast.LENGTH_SHORT).show();
            break;

        case R.id.checkOut:
            Toast.makeText(MainActivity.this, "Check-Out", 
Toast.LENGTH_SHORT).show();
            break;

        case R.id.applyOff:
            break;

        case R.id.reportBug:
            break;

        case R.id.manageProfile:
            showMyFragment();
            break;

        case R.id.logout:
            break;

        default:
            break;
    }

}

public void showMyFragment(){
    Fragment menuFragment = new MyCustomFragment();
       FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frameLayout, menuFragment).commit();
}
public class MyCustomFragment extends Fragment {

//this text view is got from the xml below 
private TextView tvTest;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
              return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        bindView(view);

        //start your logic here
    }



    private void bindView(View view){
     tvTest = view.findViewById(R.id.tv_fragment_test);

     Toast.makeText(getContext(),tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
    }
  }
// could be any kind of layout
  // for example

  <?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:id="@+id/tv_fragment_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I'm showing on a fragment"
        android:src="@drawable/app_img_small" />

</LinearLayout>