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

青春壹個敷衍的年華 提交于 2019-12-06 20:33:04

问题


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)

回答1:


Change android.support.v7.widget.AppCompatButton to Button.

It will work for u.

Other way.

You can use setOnClickListener in Your Activity for Click event.




回答2:


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.



来源:https://stackoverflow.com/questions/44383140/could-not-find-a-method-onclickview-in-the-activity-class-tintcontextwrapper-f

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