片段中的onClick方法永远不会被调用

Sar*_*nce 5 java android android-studio

我试图在我的片段中设置一个onClickListener.

public class HomeFragment extends Fragment implements View.OnClickListener {

Button btn_eventList;
public HomeFragment() {
    // Required empty public constructor
}


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

    btn_eventList = (Button) view.findViewById(R.id.buttonEventList);
    btn_eventList.setOnClickListener(this);

    return view;
}

@Override
public void onClick(View v) {
    btn_eventList.setText("TEST");
    Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show();
}

}
Run Code Online (Sandbox Code Playgroud)

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nvd"
    android:layout_height="match_parent"
    android:layout_width="match_parent">



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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

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

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:fontFamily="sans-serif"
                    android:gravity="center"
                    android:text="test"
                    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
                    />

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textView2"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="1dp"
                    android:gravity="center"
                    android:text="test"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="45dp"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/buttonEventList"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.v7.widget.RecyclerView>

        </LinearLayout>

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/lst_nav_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        >

    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么当我按下按钮时onClick方法永远不会被调用?我错过了什么吗?

Pie*_*ley 4

编辑

由于塞文让我注意到我没有给你一个解决方案,而是一个实现目标的替代方法,所以我正在编辑这个。

第一:你的代码必须有效

您发布的代码是正确的

现在,做一些检查:

  • 检查在调试模式下按钮是否不为空(应该抛出异常)
  • 检查在调试模式下是否到达 onClick,即使文本没有更改,即使 toast 没有显示。
  • 检查您是否正确显示了片段。这是有关如何创建片段的官方文档。
  • 检查片段是否可单击以及上面是否有任何内容(简单地说,单击后的按钮会显示经典的“单击”动画?)
  • 检查 xaml 代码,您使用的 id 是否分配给了正确的按钮。

最后进行以下一些操作:

  • 清理你的项目
  • 重建您的项目
  • 检查 xaml 和 java 代码中是否有任何警报(在基本配置中,线上有黄色标记)
  • 卸载应用程序并重新安装(几乎相同,但由于我们不知道您的项目,它仍然可以提供帮助)

之后,请告诉我们

可能的解决方案:

你有一个recycleview位于button. 您在 a 内设置了此视图的match parent高度和宽度relativelayout,这意味着此视图将覆盖按钮。

删除这个空recycleview,代码就可以工作了