Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find a method onClick(View) in the activity class TintContextWrapper for onClick if using themes

I'm getting the Could not find a method onClick(View) in the activity class android.support.v7.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton exception recently after adding the ability to select a dark or light theme for my app.

I do set the theme in the manifest and then I use following BaseActivity:

public abstract class BaseActivity extends AppCompatActivity
{
    private final int mLightTheme;
    private final int mDarkTheme;

    public BaseActivity(int lightTheme, int darkTheme)
    {
        mLightTheme = lightTheme;
        mDarkTheme = darkTheme;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        setTheme(MainApp.getPrefs().darkTheme() ? mDarkTheme : mLightTheme);
        super.onCreate(savedInstanceState);
    }
}

Any ideas what else could cause this issue? I got the issue from a device running 4.2.2...

NOT working solutions

  • similar issues suggest to remove the theme tag from the xml file => I'm not using it in my xml, so this is not the case for me

Stacktrace

java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class android.support.v7.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'btBack'
    at android.view.View$1.onClick(View.java:3602)
    at android.view.View.performClick(View.java:4220)
    at android.view.View$PerformClick.run(View.java:17513)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5455)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
    at java.lang.Class.getConstructorOrMethod(Class.java:460)
    at java.lang.Class.getMethod(Class.java:915)
    at android.view.View$1.onClick(View.java:3595)
    ... 11 more
    java.lang.NoSuchMethodException: onClick [class android.view.View]
    at java.lang.Class.getConstructorOrMethod(Class.java:460)
    at java.lang.Class.getMethod(Class.java:915)
    at android.view.View$1.onClick(View.java:3595)
    at android.view.View.performClick(View.java:4220)
    at android.view.View$PerformClick.run(View.java:17513)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5455)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733)
    at dalvik.system.NativeStart.main(Native Method)
like image 344
prom85 Avatar asked Jun 06 '17 06:06

prom85


1 Answers

For me magically worked setCompatVectorFromResourcesEnabled(true); like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    super.onCreate(savedInstanceState);
    ...
}

Actually, it should be called only once and this could be done in Application class, not necessarily in Activity.

Honestly, I had this code line for other reasons but when tried to remove it, I got exactly this bug with onClick. So I put this line back and wow! Like a charm.

like image 129
apc Avatar answered Sep 25 '22 11:09

apc