Fragment中一直报找不到点击事件

Fragment中一直报找不到点击事件

# 具体遇到的问题
在购物车tab里面,购物车tab的内容为fragment, 为顶部的本地商家/外地商家/默认排序添加了点击事件,用来切换不同的fragment, 但是一直报找不到onClick方法。

# 报错信息的截图
http://img1.sycdn.imooc.com//climg/5fe615a7096a98b609160353.jpg# 相关课程内容截图

# 尝试过的解决思路和结果
直接查找radioButton的id绑定click事件却可以

# 粘贴全部相关代码,切记添加代码注释(请勿截图)


private void merchant_click(View view){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
   switch (view.getId()){
case R.id.local_merchant:
transaction.replace(R.id.tab_cart_merchant_container, fragment_local);
           break;
       case R.id.foreign_merchant:
transaction.replace(R.id.tab_cart_merchant_container, fragment_foreign);
           break;
       case R.id.default_btn:
transaction.replace(R.id.tab_cart_merchant_container, fragment_default);
           break;
   }
transaction.commit();
}


<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/local_merchant"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="本地商家"
android:button="@null"
android:textSize="20sp"
android:gravity="center"
android:checked="true"
android:onClick="merchant_click"/>

<RadioButton
android:id="@+id/foreign_merchant"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="外地商家"
android:button="@null"
android:textSize="20sp"
android:gravity="center"
android:onClick="merchant_click"/>

<RadioButton
android:id="@+id/default_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="默认排序"
android:button="@null"
android:textSize="20sp"
android:gravity="center"
android:onClick="merchant_click"/>
</RadioGroup>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕雪 2020-12-26 17:30:35

同学,你好。1)把访问权限改为public

http://img1.sycdn.imooc.com//climg/5fe70288090e863706310235.jpg

2)请确认,视图xml与fragment是否对应。祝学习愉快!

  • 提问者 慕侠8177421 #1

    权限改成public了,同时也再三确认对应关系还是不行。
    在网上百度相关问题,有回答说是fragment无法直接响应onClick事件,试了其他的控件也不行。
    不确定是不是真的无法响应onClick事件

    2020-12-26 18:13:43
  • 好帮手慕雪 回复 提问者 慕侠8177421 #2

    fragment对应的xml里确实是不能用android:onClick。解决方法:同学可以在fragment中

    findViewById()找到对应控件,然后setOnClickListener()来监听事件。


    2020-12-28 10:39:22
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师