Android-onclick不断在模拟器api 19上崩溃,在其他地方正常工作

时间:2018-07-05 16:18:29

标签: android

我在真实设备上的API 23,API 25仿真器,API 26仿真器以及API 19仿真器上尝试了我的应用程序。它一直在最后一个崩溃,而在其他情况下则正常工作。

从主要节日 minSdkVersion 19 targetSdkVersion 26

我的XML定义了onclick,就像我为更改字体样式的自定义按钮-

<com.ta.squltra.MyButton
    android:id="@+id/okButton"
    android:layout_width="@dimen/_60sdp"
    android:layout_height="@dimen/_40sdp"
    android:text="OK"
    android:background="@drawable/chalkbutton"
    android:onClick="okButtonClicked"
    style="@style/taWhiteLargeButtonText"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    />

我的.java用于上述xml-

public void okButtonClicked(View v){
    Intent intent = new Intent(this, QuizActivity.class);
    intent.putExtra(EXTRA_CATEGORY, gameMode);
    startActivity(intent);
    finish();

}

还有MyButton类的代码,以防万一-

public class MyButton extends android.support.v7.widget.AppCompatButton 
{
public MyButton(Context context) {
    super(context);
    init();
}

public MyButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public MyButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    init();
}

private void init(){
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Filmcryptic.ttf");
    setTypeface(tf);

}
}

最后是logcat输出(已编辑)-

--------- beginning of /dev/log/system
07-05 21:34:21.078 1538-1538/com.ta.squltra E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ta.squltra, PID: 1538
    java.lang.IllegalStateException: Could not find a method okButtonClicked(View) in the activity class android.support.v7.widget.TintContextWrapper for onClick handler on view class com.ta.squltra.MyButton with id 'okButton'
        at android.view.View$1.onClick(View.java:3810)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: okButtonClicked [class android.view.View]
        at java.lang.Class.getConstructorOrMethod(Class.java:472)
        at java.lang.Class.getMethod(Class.java:857)
        at android.view.View$1.onClick(View.java:3803)
        at android.view.View.performClick(View.java:4438) 
        at android.view.View$PerformClick.run(View.java:18422) 
        at android.os.Handler.handleCallback(Handler.java:733) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5001) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
        at dalvik.system.NativeStart.main(Native Method) 
07-05 21:34:21.078 468-781/system_process W/ActivityManager:   Force finishing activity com.ta.squltra/.InfoScreenActivity
07-05 21:34:21.462 474-474/? D/dalvikvm: GC_FOR_ALLOC freed 508K, 16% free 3139K/3708K, paused 9ms, total 9ms

1 个答案:

答案 0 :(得分:0)

扩展

  

按钮

代替

  

android.support.v7.widget.AppCompatButton

在您的MyButton类中。