【问题标题】:onClick method in fragment never gets called片段中的 onClick 方法永远不会被调用
【发布时间】:2016-09-30 22:53:05
【问题描述】:

我试图在我的片段中设置一个 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 方法永远不会被调用?我错过了什么吗?

【问题讨论】:

  • 你这样做的方式很奇怪,只是做一个单独的监听器类或匿名监听器。
  • 你能给我们看看 fragment_home 布局吗?
  • 另外:您是否尝试过重建?你确定你正在使用这个片段吗? 这段代码应该可以工作。
  • 我认为您的代码没有问题......请确保您的布局中有一个 id 为 buttonEventList 的按钮(fragment_home)
  • 正如 RissmonSuresh 所说,代码没有任何问题。我刚刚测试了您的解决方案及其工作原理。请像@Selvin 建议的那样发布 fragment_home 布局,并告诉我们您如何使用 Fragment

标签: java android android-studio


【解决方案1】:

编辑

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

首先:您的代码必须有效

您发布的代码是正确的

现在,做一些检查:

  • 在调试模式下检查按钮是否不为空(应该抛出异常)
  • 检查您是否在调试模式下到达 onClick,即使文本没有更改,即使 toast 没有显示。
  • 检查您是否正确显示片段。 Here is the official documentation about how to create a fragment.
  • 检查片段是否可点击以及上面是否有任何东西(简单地说,按钮一旦点击就会显示经典的“点击”动画?)
  • 如果您使用的 id 分配给了正确的按钮,请检查 xaml 代码。

最后做如下一些操作:

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

之后,让我们知道

可能的解决方案:

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

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

【讨论】:

  • 这将如何帮助解决他的真正问题,即“onClick 方法永远不会被调用”?
  • @Selvin 因为看到他给我们的代码很糟糕,问题一定出在监听器调用中(我真的希望他做了类似调试的事情)。 Sicne 他发布的代码必须有效,检查它是否也可以给我们一个巨大的提示,告诉我们真正的问题在哪里。顺便说一句,这种方式有效,而且几乎是一样的,所以这是一个解决方案
  • 几乎一样不,不一样...它不必要地创建匿名类的实例... 所以这是一个解决方案 i> 不,这不能解决他的主要问题
  • 我在我的项目中使用与您相同的解决方案 :) (出于您所写的原因 - 区分所有按钮)...但是...这不是问题的答案...
  • @Selvin 是的,你是对的。我从根本上改变了答案,现在它更像是一个“调试指南”,但我认为这就是他所需要的
【解决方案2】:

编辑

在发布您的布局后,我可以完全确定您的 REcyclerView 占用了您布局的所有空间,从而从后面获取了所有触摸输入。

解决方案:

您可以删除回收器视图或重新排列并将其添加到最后一个 RelativeLayout 并使用 layout_below 并将其放置在 LinearLayout 下方

【讨论】:

  • 这将如何帮助解决他的真正问题,即“onClick 方法永远不会被调用”?
  • 疑难解答...然后我们可以根据结果继续调查(排除过程)
  • Programming by permutation ,不,谢谢!
  • 不幸的是这没有帮助
  • 谢谢你这就是问题所在。我重新安排了。顺便说一句,您之前建议的代码是更好的方法吗?如果我有你的方式,我是否必须删除'implements View.OnClickListener'?
【解决方案3】:

问题是你没有正确实现这个方法。 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;
    }
}

【讨论】:

  • 没关系,因为只有 1 次调用 setOnClickListener(HomeFragment .this) 以及这将如何帮助解决他的真正问题,即“onClick 方法永远不会被调用”?
  • @Selvin,哦,谢谢指正,这是我的疏忽:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多