在片段中的onClick方法从未被调用

5
我试图在我的片段中设置一个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();
}

}

这是我的布局:

<?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>

有人知道为什么我点击按钮时,onClick方法从未被调用吗?我是否漏掉了什么?


你现在的做法有点奇怪,最好是创建一个单独的监听器类或匿名监听器。 - Shadov
2
你能展示一下fragment_home的布局吗? - Selvin
1
另外,你尝试过重新构建吗?你确定你正在使用这个片段吗?这段代码应该可以工作。 - Selvin
我看你的代码没有问题......请确保在你的布局(fragment_home)中有一个id为buttonEventList的按钮。 - Rissmon Suresh
@SaraLince,请查看下面的答案,我已经找到了解决你问题的方法。 - Ivan Alburquerque
显示剩余2条评论
3个回答

4

编辑

由于Sevin提醒我并不是给你提供解决方案,而是为了实现目标的另一种方式,所以我进行了编辑。

首先,你的代码必须可行

你发布的代码是正确的

接下来,进行一些检查:

  • 检查调试模式中按钮是否为空(应该会抛出异常)
  • 检查调试模式中是否已经触发 onClick,即使文本没有改变,toast 也没有显示。
  • 检查是否正确显示片段。 这里是有关如何创建片段的官方文档。
  • 检查片段是否可点击,并且是否有任何内容覆盖它(简单地说,按钮一旦被点击就会显示经典的“点击”动画?)
  • 在 xaml 代码中检查您使用的 ID 是否分配给了正确的按钮。

最后执行以下操作:

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

执行完以上操作后,请告诉我们。

可能的解决方法:

您有一个位于 button 上方的 recycleview。在 relativelayout 中,将此视图设置为 match parent 的高度和宽度,这意味着该视图将覆盖按钮。

删除此空的 recycleview,代码就可以正常运行。


这将如何帮助他解决他的真正问题,即“onClick方法从未被调用”? - Selvin
@Selvin 因为我们看到他给出的糟糕代码,问题肯定出在监听器调用上(我真的希望他做了像调试之类的事情)。因为他发布的代码应该是可以工作的,检查这个是否也起作用将为我们提供解决真正问题的巨大提示。顺便说一句,这种方法可行,并且与其它方法几乎相同,所以这是一个解决方案。 - Pier Giorgio Misley
“它几乎是一样的东西”不,它并不一样...它会不必要地创建匿名类的实例...“所以这是一个解决方案”不,它并没有解决他的主要问题。 - Selvin
@Selvin 是的,你说得对。我彻底修改了答案,现在更像是一个“调试指南”,但我认为这正是他需要的。 - Pier Giorgio Misley
@SaraLince请查看“解决方案”标题下的编辑答案。 - Pier Giorgio Misley
显示剩余2条评论

1

编辑

在发布您的布局后,我可以确定地得出结论:您的REcyclerView占用了布局的所有空间,因此占用了背后的所有触摸输入。

解决方案:

您可以删除RecyclerView,或重新排列它并将其添加到最后一个RelativeLayout中,并使用layout_below将其放置在LinearLayout下方。


这将如何帮助他解决他的真正问题,即“onClick方法从未被调用”? - Selvin
故障排除...然后我们可以根据结果继续进行调查 (逐步排除法) - Ivan Alburquerque
编程排列,不用了,谢谢! - Selvin
很遗憾,这并没有帮助。 - Sara Lince
谢谢,那就是问题所在。我重新排列了一下。顺便问一下,你之前建议的代码是更好的方法吗?如果按照你的方式,我需要删除“implements View.OnClickListener”吗? - Sara Lince
显示剩余2条评论

-1
问题是您没有正确实现此方法。onClick()方法不知道具体点击了哪个视图。将您的onClick()方法更改为以下形式:
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.your_button_id: 
            btn_eventList.setText("TEST");
            Toast.makeText(getActivity(), "TEST" , Toast.LENGTH_SHORT).show();
            break;
    }
}

由于只有一个 setOnClickListener(HomeFragment.this) 的调用,这并不重要,而且这如何帮助他解决他的真正问题,即 "onClick 方法从未被调用"? - Selvin
@Selvin,哦,谢谢你的纠正,是我粗心大意了 :) - R. Pihariev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接