底部标签更改片段

时间:2018-05-15 19:41:34

标签: java android fragment

我在更改标签时一直试图更改我的片段而且我不知道它为什么不起作用,我已经检查了很多视频,但它仍然是没有工作,我不知道我的错误是什么,如果你可以指出它(我已经尝试过获得支持片段,但它也没有工作,只是变红),这是我的代码

package com.example.whatsoever.ttravel;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.view.MenuItem;

//import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentManager;


public class MainActivity extends Activity {


private BottomNavigationView.OnNavigationItemSelectedListener 
mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:

                category_map map = new category_map();
                FragmentManager fragmentManager= getFragmentManager();
   fragmentManager.beginTransaction(R.id.a,map,map.getTag()).commit();


               return true;
            case R.id.navigation_dashboard:

                return true;

        }
        return false;
    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

 }

这里是我的xml文件,我想添加我的片段

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/a"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

  </android.support.constraint.ConstraintLayout>

这是错误的图像 enter image description here

请帮助

2 个答案:

答案 0 :(得分:0)

您正在尝试将参数传递给beginTransactions()方法。这个方法不带任何参数,你可以在这里看到:

https://developer.android.com/reference/android/app/FragmentManager#begintransaction

尝试此行fragmentManager.beginTransaction().commit();并查看它是否已编译。你可能想要做的是将这些参数传递给片段,或者你正在寻找片段事务上另一个名为replace的事务方法:

https://developer.android.com/reference/android/app/FragmentTransaction#replace

答案 1 :(得分:0)

你必须改变

FragmentManager fragmentManager= getFragmentManager();

以下

FragmentManager fragmentManager= getSupportFragmentManager();