无法找到一个方法onClick

无法找到一个方法onClick

本文介绍了无法找到一个方法onClick_Foo(查看) - 在Android上运行棒棒堂第一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,是一岁左右,是在公测Play商店,经历了几十个版本了。突然,我发现了一个错误:

我对每一个在我的XML定义的7个按键中收到此错误。从昨天开始我已经更新appcompat-V7从21.0.3至22.0.0,但也进行了升级,从奇巧我的测试装置,棒棒堂首次。

我有双重检查拼写,字母大小写,没有秋后算账的解释这一点。下面是有关code的样本。让我知道如果你觉得更将是有益的。 (该活动有915线code和XML 186,所以不要问了整个事情)。在Verizon的注意事项4运行棒棒堂5.0.1测试

activity_pick.xml:

 <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:主题=@风格/ AppTheme
    工具:上下文=com.myapp.Pick
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:方向=垂直>

        <按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / Ratings_Button
            机器人:TEXTSIZE =16DP
            机器人:文本=@字符串/ Pick_Ratings_Button
            机器人:的onClick =onClick_Ratings
            机器人:背景=@机器人:可绘制/ btn_default/>
< / LinearLayout中>
< /滚动型>
 

Pick.java:

 公共类Pick_Restaurant扩展ActionBarActivity {
    公共无效onClick_Ratings(视图v){
        意向意图=新的意图(mContext,Ratings.class);
        startActivityForResult(意向,额定值);
    }
}
 

build.gradle:

 应用插件:com.android.application

安卓{
    compileSdkVersion 22
    buildToolsVersion22.0.1

defaultConfig {
    的minSdkVersion 15
    targetSdkVersion 22
    版本code 59
    VERSIONNAME0.6.4
}

...

依赖{
    编译文件树(包括:['的* .jar'],DIR:库)
    编译com.android.support:appcompat-v7:22.0.0
    编译com.google.android.gms:播放服务:7.0.0
    编译文件(库/ mobileservices-1.1.5.jar)
}
 

在日志已满错误:

  04-08 17:06:40.578 3508-3508 / com.myapp.debug E / AndroidRuntime:致命异常:主要
    工艺:com.myapp.debug,PID:3508
    java.lang.IllegalStateException:找不到在活动课android.view.ContextThemeWrapper为onclick处理方法onClick_Ratings(视图)的视图类android.widget.Button ID为'Ratings_Button
            在android.view.View $ 1.onClick(View.java:4234)
            在android.view.View.performClick(View.java:5191)
            在android.view.View $ PerformClick.run(View.java:20916)
            在android.os.Handler.handleCallback(Handler.java:739)
            在android.os.Handler.dispatchMessage(Handler.java:95)
            在android.os.Looper.loop(Looper.java:145)
            在android.app.ActivityThread.main(ActivityThread.java:5974)
            在java.lang.reflect.Method.invoke(本机方法)
            在java.lang.reflect.Method.invoke(Method.java:372)
            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388)
            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     致:java.lang.NoSuchMethodException:onClick_Ratings [类android.view.View]
            在java.lang.Class.getMethod(Class.java:665)
            在android.view.View $ 1.onClick(View.java:4227)
在android.view.View.performClick(View.java:5191)
在android.view.View $ PerformClick.run(View.java:20916)
在android.os.Handler.handleCallback(Handler.java:739)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:145)
在android.app.ActivityThread.main(ActivityThread.java:5974)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
 

解决方案

做一些搜索,它看起来这是与Android 5.0一个新的问题,可能你appcompat的更新。

有人发现,在这里为他们工作的解决方案:android 5和的onClick在XML布局

走进你的XML文件中的设计选项卡,并确保它们与正确的活动有关。

从上链接后其他的答案,有人提到,取消主题从布局XML为他们修复了这个问题。

因此​​,删除主题从您的布局:

 <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    <  - 机器人:主题=@风格/ AppTheme - > <! - 删除这一点 - >
 

和添加的主题,在AndroidManifest.xml中,而不是:

 安卓主题=@安卓风格/ AppTheme
 

I have an app that is about a year old, is on the Play store in beta, has gone through dozens of revisions. All of a sudden I'm getting an error:

I'm getting this error on every one of the 7 buttons defined in my XML. Since yesterday I've updating appcompat-v7 from 21.0.3 to 22.0.0 but also upgraded my testing device from KitKat to Lollipop for the first time.

I've double-checked spellings, capitalizations, none of the usual suspects explains this. Here's a sample of the relevant code. Let me know if you feel more would be helpful. (The activity has 915 lines of code and the xml 186, so don't ask for the whole thing). Testing on a Verizon Note 4 running Lollipop 5.0.1

activity_pick.xml:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:theme="@style/AppTheme"
    tools:context="com.myapp.Pick"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Ratings_Button"
            android:textSize="16dp"
            android:text="@string/Pick_Ratings_Button"
            android:onClick="onClick_Ratings"
            android:background="@android:drawable/btn_default"/>
</LinearLayout>
</ScrollView>

Pick.java:

public class Pick_Restaurant extends ActionBarActivity {
    public void onClick_Ratings (View v) {
        Intent intent = new Intent(mContext, Ratings.class);
        startActivityForResult(intent,RATINGS);
    }
}

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 59
    versionName "0.6.4"
}

...

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
    compile files('libs/mobileservices-1.1.5.jar')
}

Full Error on Log:

04-08 17:06:40.578    3508-3508/com.myapp.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myapp.debug, PID: 3508
    java.lang.IllegalStateException: Could not find a method onClick_Ratings(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'Ratings_Button'
            at android.view.View$1.onClick(View.java:4234)
            at android.view.View.performClick(View.java:5191)
            at android.view.View$PerformClick.run(View.java:20916)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5974)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     Caused by: java.lang.NoSuchMethodException: onClick_Ratings [class android.view.View]
            at java.lang.Class.getMethod(Class.java:665)
            at android.view.View$1.onClick(View.java:4227)
            at android.view.View.performClick(View.java:5191)
            at android.view.View$PerformClick.run(View.java:20916)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5974)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
解决方案

Doing some searching, it looks like this is a new issue with Android 5.0, and possibly your update of appcompat.

Someone found a solution that worked for them here:android 5 and onClick in xml layout

Go into the Design tab of your xml files, and make sure that they are associated with the correct Activity.

From other answers on that linked post, someone mentions that removing the Theme from the layout xml fixed this issue for them.

So, remove the theme from your layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    <!--android:theme="@style/AppTheme"--> <!-- Remove this -->

And add the theme in the AndroidManifest.xml instead:

android:theme="@android:style/AppTheme"

这篇关于无法找到一个方法onClick_Foo(查看) - 在Android上运行棒棒堂第一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:50