Could not find a method onClick_Foo(View) - first time running on Android Lollipop

僤鯓⒐⒋嵵緔 提交于 2019-11-30 08:28:45
Daniel Nugent

It looks like this is a new issue with Android 5.0.

From this anwer, removing the Theme from the layout xml fixed this issue for them.

So in your case, 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 -->
    <!--................-->
  </ScrollView>

And add the theme in the AndroidManifest.xml instead:

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

I have just answered a similar question here

Basically, since Android 5.0 individual views can be themed.

To facilitate this, a ContextThemeWrapper is used to modify the underlying theme assigned to the Activity and assigned as the Context of the View. Since this Context is not your Activity anymore (the Activity has to be separate because it still has to return the original theme) the callbacks don't exist and you get the error you see.

If you don't really want to theme individual views the obvious solution is to not do so and theme the activity instead, as already suggested.

If you do indeed want to theme individual views, it appears that the android:onClick attribute cannot be used and you will have to fall back to manually assign an OnClickListener.

The question is, why did this work pre Lollipop? I can only speculate that because the functionality to theme individual views didn't exist, applying a theme attribute to a view would just change the default theme on the Activity as well.

Such things usually happens when you declare onClick in xml like you did:

android:onClick="onClick_Ratings"

Just make sure you are using this layout in that activity. Because the exception is clearly saying that your activity don't have the corresponding method which you did show you have:

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

Also I guess you should declare the activity in your xml like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SendMessageActivity">

And actually log says:

Could not find a method onClick_Ratings(View) in the activity class android.view.ContextThemeWrapper

there is something wrong since android.view.ContextThemeWrapper is not an Activity and not the activity from your xml com.myapp.Pick (I assume Pick is an activity and not the fragment). Maybe try to clean up the project, ivalidate caches and restart.
If nothing helps I suggest you to return back to previuos version of support lib you'd mentioned OR to set onClickListener in code instead of xml.

Removing android:theme works. But this is caused because using android:onclick in xml. If you use onClick in Java file, you can still use android:theme.

According to your code,

activity_pick.xml [Remove onclick here]

 <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:background="@android:drawable/btn_default" />

Go to your Java File, that is, pick.java in your case:

    final Button button = (Button) findViewById(R.id.Ratings_Button);

    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent activityChangeIntent = new Intent(CurrentActivity.this, SecondActivity.class);
            SplashScreen.this.startActivity(activityChangeIntent);
        }
    });

Experienced the same error, app has stopped working, when material button was clicked programmatically helped me. Check it out :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!