Java 安卓-按钮赢了';t开关片段

Java 安卓-按钮赢了';t开关片段,java,android,android-fragments,button,Java,Android,Android Fragments,Button,我一直在尝试让我的浮动动作按钮在片段中工作,它会切换到下一个片段,并将动画滑动到下一个片段,但由于某种原因,当我单击浮动动作按钮时,它什么也不做。有人能帮我指出原因吗 这是我的片段类 import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; import android.support.v4.app.Fra

我一直在尝试让我的浮动动作按钮在片段中工作,它会切换到下一个片段,并将动画滑动到下一个片段,但由于某种原因,当我单击浮动动作按钮时,它什么也不做。有人能帮我指出原因吗

这是我的片段类

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScoutFragment extends Fragment {

    FloatingActionButton addDataScout;

    public ScoutFragment() {
        // Required empty public constructor
    } //End of ScoutFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scout, container, false);

        //Setup Floating Action Button
        FloatingActionButton addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
        addDataScout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AddScoutDataFragment fragment = new AddScoutDataFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            } //End of onClick
        }); //End of setOnClickListener

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_scout, container, false);
    } //End of onCreateView
} //End of class
这是xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment">

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="61dp"
            android:layout_height="57dp"
            android:src="@mipmap/ic_action_add"
            app:backgroundTint="#F28016"
            app:elevation="5dp"
            app:borderWidth="0dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="12dp"
            android:layout_marginRight="10dp"
            android:scaleType="fitCenter"
            android:clickable="true">
        </android.support.design.widget.FloatingActionButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Western University"
            android:id="@+id/testText"
            android:layout_centerInParent="true"
            android:textSize="24dp"
            android:textStyle="bold" />
    </RelativeLayout>
</FrameLayout>


我也在为我的项目使用
NavigationDrawer
是的!它什么也不做,因为:

  • fragment\u scout
片段事务的
replace
方法将替换容器中的任何视图(R.id.data\u add),但是
FragmentTransaction
不管理容器

修正:

  • 将替换操作移动到片段的父视图,如下所示

  • 是的!它什么也不做,因为:

    • fragment\u scout
    片段事务的
    replace
    方法将替换容器中的任何视图(R.id.data\u add),但是
    FragmentTransaction
    不管理容器

    修正:

    • 将替换操作移动到片段的父视图,如下所示

    您将容器名称命名为
    fragment\u container
    ,但它在哪里?您需要做的是将FrameLayout的id指定为
    fragment\u container


    android:id=“@+id/fragment\u container”
    添加到框架布局中,您就可以开始了。

    您将容器名称命名为
    fragment\u container
    ,但它在哪里?您需要做的是将FrameLayout的id指定为
    fragment\u container


    android:id=“@+id/fragment\u container”
    添加到FrameLayout,您就可以开始了。

    我将id更改为
    android:id=“@+id/layoutScout”
    然后将代码更改为
    fragmentTransaction.replace(R.id.layoutScout,fragment)但是没有任何改变,我点击按钮,另一个片段没有弹出up@Samer阿拉比更改
    返回充气机。充气(R.layout.fragment\u scout,容器,假)带有
    返回视图为什么要膨胀两次?这有点奏效,另一个片段弹出,但另一个仍然存在,我该怎么做才能修复that@SamerAlabi尝试在
    onCreate
    方法中调用
    container.removeallview()
    。既然我的解决方案奏效了,你能接受我的回答吗?像这样?(对于Android开发来说真的很陌生,抱歉)
    public void onCreate(ViewGroup容器,Bundle savedInstanceState){container.removeAllViews();}//onCreate的结束
    我将id更改为
    Android:id=“@+id/layoutScout”
    然后将代码更改为
    fragmentTransaction.replace(R.id.layoutScout,fragment)但是没有任何改变,我点击按钮,另一个片段没有弹出up@Samer阿拉比更改
    返回充气机。充气(R.layout.fragment\u scout,容器,假)带有
    返回视图为什么要膨胀两次?这有点奏效,另一个片段弹出,但另一个仍然存在,我该怎么做才能修复that@SamerAlabi尝试在
    onCreate
    方法中调用
    container.removeallview()
    。既然我的解决方案奏效了,你能接受我的回答吗?像这样?(对于Android开发来说真的是新手对不起)
    public void onCreate(ViewGroup容器,Bundle savedInstanceState){container.removeAllViews();}//onCreate的结尾
    你可以这样做:你可以检查我的评论。你可以这样做:你可以检查我的评论。