Android EditText onClick Listener defined in layout doesn't work with unclear exception

I want to define an onClick listener for EditText in the canout activity's Layout-XML, but it always fails with an unclear Exception.

The layout is introduced by setContentView() in onCreate - The onCreate line of my activity. I am not using the snippet here, and I am well aware that the onClick Listener XML onClick does not work for snippets.

For testing purposes, I added the same handler method to ImageView, which is next to EditText. A handler works there, it fails on EditText. So this is something special about EditText, not the general problem of the mislocated handler method.

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" /> 

When I click on ImageView, the corresponding method executes and DialogFragment starts. When I click on 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) 

I am currently testing the Samsung S4 API (Android 5.0.1) 21 and the AppCompat library in version v7 22.1.0

Adding an onClick Listener programmatically in the onCreate method would be a solution, but I wanted to avoid it, since it leads to a single onClick method with a large switch statement and an XML-based onClick- handler leads to a more readable and explicit code, which I prefer.

Have there been additional studies:

Now I can reproduce the error behavior and the correct behavior (see below). This seems to be a bug (or function) introduced with appcompat-v7 22.1.0 and 22.1.1.

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

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

The start of the application touched EditText -> Crash

Changed my build.gradle:

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

to

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

and Toast displayed.

Any ideas?

+6
source share
3 answers

Perhaps this helps others save time in finding a solution. I raised a question for appcompat7, which was adopted with the following comment:

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

More details: https://code.google.com/p/android/issues/detail?id=174871

So, this is appcompat error / problem, and I will step back from adding the onClick programmer.

+10
source

You need to define the onCreationClicked(View) method in Activity not in Fragment . If you want Fragment handle clicks, you need to remove onClick in your XML and programmatically process the listener.

+3
source

I used the following tip from # 5 on Issue 174871 :

change: <EditText .../> in <android.widget.EditText .../>

So Appcompat no longer handles onClick

0
source

Source: https://habr.com/ru/post/987986/


All Articles