解决android4.4的bug NoSuchMethodException View.onClick

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wxz1179503422/article/details/81476238

Android 4.4版本 xml中的onClick属性不兼容support库。

用户反馈崩溃,用自己手机测试没有问题,在优测上找了一台相同版本的手机测试,还真找到了问题,View.onClick方法抛NoSuchMethod方法,当时就很奇怪,怎么会找不到onClick方法呢,我又去看了bugly上面的这个方法,搜索出来,全是android 4.4的手机。这里写图片描述
我仔细检查代码,崩溃的地方时使用了AppCompatCheckBox,,我并没有设置onClick方法,为什么会崩溃呢,仔细检查之后发现,是在xml中定义的style,引入了onClick属性,查看checkbox源码,checkbox在setCheck()之后会调用performClick(),调用onClick方法。在StackOverflow上找到原因,是xml中的onClick不兼容support库AppCompatButton android:onClick Could not find a method exception
以及这个bugtracker(https://issuetracker.google.com/issues/37108938)
最后去掉xml中的onClick属性,运行测试就没有问题了。
引用原话

I have found some explanation for this issue. It was filed as a bug. Here is the link to bug report. https://issuetracker.google.com/issues/37108938

In a gist, they say that the issue was fixed in API 24 and onClick is not compatible with support library. I have added a comment to the thread about kitkat issue.


感谢前辈们,努力前行!

猜你喜欢

转载自blog.csdn.net/wxz1179503422/article/details/81476238
今日推荐