Android:从旧片段显示的片段

时间:2017-07-20 08:15:06

标签: android android-fragments

当我调用第二个片段时,我有两个片段在彼此之上我仍然是第一个片段中的按钮保持可见和功能。

我尝试了很多方法,但没有工作

This一个人给了我这个错误:

  

java.lang.IllegalArgumentException:找不到片段SecondFragment的id 0x7f0e0004(* .test:id / customId)的视图{adef947#0 id = 0x7f0e0004}

This

  

java.lang.IllegalArgumentException:找不到用于片段SecondSFragment的id 0x7f0e00ae(sy.aya.ayaisp.test:id / gatesViewpager)的视图{adef947#0 id = 0x7f0e00ae}

我的代码如下:

  

FirstFragment.java

public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {

        View myView = inflater.inflate(R.layout.first_fragment, container, false);
        Button FBtn = (Button) myView.findViewById(R.id.Fbutton);
FBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("second", "f button");
                SecondFragment secondFragment = new secondFragment();
                secondFragment.setArguments(bundle);

                FragmentTransaction ft = getChildFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_in_up);
                ft.replace(R.id.s_con, secondFragment).addToBackStack("back").commit();
            }
        });
  

SecondFrag.java

       @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      String bundleString = this.getArguments().getString("second");
      View view = inflater.inflate(R.layout.s_rv, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.s_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
if (bundleString == "agents button") {
  recyclerView.setAdapter(new SecondAdapter(getContext()));
}
}
  

first_fragment.xml

<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/s_con"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
    android:id="@+id/Fbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="secondFragment"/>
    </android.support.constraint.ConstraintLayout>
  

s_rv.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <sy.aya.ayaisp.ExpandableRecyclerView
        android:id="@+id/s_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:clipToPadding="false"
        android:scrollbars="vertical" />
</FrameLayout>

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

在第二个片段布局&#34; s_rv&#34;在xml的根元素中添加一些backgroundclickable="true"focusable="true"

在第一个片段布局中,将您的按钮包裹在LinearLayout中。两天前,我也遇到了同样的问题,之后就解决了。

答案 1 :(得分:0)

这是因为在你的第一个片段中,按钮的id是&#34; pos_agents_button&#34;但是在findViewById中你试图找到&#34; R.id.Fbutton&#34;。

更改

Button FBtn = (Button) myView.findViewById(R.id.Fbutton);

 Button FBtn = (Button) myView.findViewById(R.id.pos_agents_button);

一切都应该正常。