在布局中定义的Andr​​oid的EditText的onClick监听器失败,晦涩异常 [英] Android EditText onClick Listener defined in Layout fails with obscure Exception

查看:451
本文介绍了在布局中定义的Andr​​oid的EditText的onClick监听器失败,晦涩异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要定义一个onClick监听器在5月份活动的布局,XML一个EditText,但它总是失败,一个不起眼的异常。

I want to define an onClick Listener for an EditText in the Layout-XML of may activity, but it always fails with an obscure Exception.

布局注射在我的活动的onCreate - 方法的setContentView()。我的不可以这里使用的是片段,我清楚地知道,XML定义的onClick 监听器没有为碎片工作。

The Layout is injected with setContentView()in the onCreate-Method of my activity. I am not using a Fragment here and I am well aware that the XML defined onClick Listener do not work for fragments.

出于测试目的,我加了相同的处理方法,以位于旁边的EditText的ImageView的。有处理程序的工作原理,它失败的EditText。因此,这是特别的东西的EditText和一个mislocated处理方法不是一般的问题。

这是我的布局文件的相关部分:

This is the relevant part of my layout file:

<ImageView
     android:layout_gravity="center_horizontal|top"
     android:layout_rowSpan="3"
     android:src="@drawable/ic_action_event"
     android:onClick="onCreationClicked"
     />

<EditText
     android:id="@+id/creation_edit"
     android:focusable="false"
     android:clickable="true"
     android:layout_gravity="fill_horizontal"
     android:hint="@string/enter_creation"
     android:onClick="onCreationClicked"
     style="@style/PickerEditText"
     />

当我点击执行相关方法的ImageView和DialogFragment开始。当我点击我的EditText得到以下异常:

When I click on the ImageView the associated method is executed and a DialogFragment is started. When I click on the EditText I get the following Exception:

    java.lang.IllegalStateException: Could not find a method onCreationClicked(View) in the activity class android.support.v7.internal.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatEditText with id 'creation_edit'
            at android.view.View$1.onClick(View.java:3994)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NoSuchMethodException: onCreationClicked [class android.view.View]
            at java.lang.Class.getMethod(Class.java:664)
            at java.lang.Class.getMethod(Class.java:643)
            at android.view.View$1.onClick(View.java:3987)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我目前在测试一台三星S4(Android版5.0.1)API 21和应用程序兼容性库版本V7 22.1.0

I am currently testing on a Samsung S4 (Android 5.0.1) API 21 and AppCompat Library in Version v7 22.1.0

添加的onClick 监听器编程中的的onCreate 方法将是一个解决方案,但我想避免这种情况,因为它导致了一个的onClick 方法用一个大的switch语句和基于XML的onClick处理程序的结果在一个更可读的和明确的code,我preFER。

Adding the onClick Listener programmatically in the onCreate method would be a solution, but I wanted to avoid this, since it results in a single onClick method with a big switch statement and the XML-based onClick-Handler result in a more readable and explicit code which I prefer.

有没有额外的研究:

现在我可以重现错误行为和正确的行为(见下文)。看来这是应用程序兼容性-V7 22.1.0和放大器引入了一个错误(或功能); 22.1.1。

Now I can reproduce the error behaviour and the correct behavior (see below). It seems this is a bug (or a feature) introduced with appcompat-v7 22.1.0 & 22.1.1.

我创建了Android Studio的一个新项目的新鲜与空白活动。 minSdk 17,targetSdk 21.添加了的EditText 空白活动如下图所示,添加处理方法到活动是这样的:

I created a new fresh project with Android Studio with Blank Activity. minSdk 17, targetSdk 21. Added the EditText to the blank Activity as shown below and added the handler method to the Activity like this:

public void onCreationClicked(View view) {
    Toast.makeText(this,"Event Handled",Toast.LENGTH_LONG).show();
}

开始在App触及的EditText - >崩溃

Started the App touched the EditText --> Crash

改变了我的build.gradle来自:

Changed my build.gradle from:

    compile 'com.android.support:appcompat-v7:22.1.1'

    compile 'com.android.support:appcompat-v7:22.0.0'

吐司所示。

任何想法?

推荐答案

也许这可以帮助别人节省时间寻找解决的办法。我提出这与以下注释接受appcompat7一个问题:

Maybe this helps others to save time with searching for a solution. I raised an issue for appcompat7 which was accepted with the following comment:

这已经被固定下一个平台的发布,但它可能无法在应用程序兼容性解决这个问题是可行的。

This has already been fixed for the next platform release, but it may not be feasible to fix this in appcompat.

详细信息:
<一href=\"https://$c$c.google.com/p/android/issues/detail?id=174871\">https://$c$c.google.com/p/android/issues/detail?id=174871

因此​​,我们确实它是应用程序兼容性的错误/问题,我将回退到编程加入onClick的监听器。

So indeed it is a bug / problem of appcompat and I will fallback to adding the onClick listener programmatically.

这篇关于在布局中定义的Andr​​oid的EditText的onClick监听器失败,晦涩异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆