为什么android:onClick属性会导致Android 4.2崩溃?

时间:2017-06-01 11:30:47

标签: android button

我的应用程序中有一个按钮,应该在单击时启动另一个活动。我在按钮中添加了一个android:onClick属性,如下所示:

 <android.support.v7.widget.AppCompatButton
        android:id="@+id/btn_show_toc_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="false"
        android:layout_centerHorizontal="true"
        android:text="@string/show_book_content"
        android:onClick="showTOC"

        />

showTOC的实现如下:

public void showTOC(View view) {
    intent= new Intent(CoverActivity.this,MainActivity.class);
    intent.putExtra(EXTRA_MESSAGE,true);
    startActivity(intent);
}

这在Nougat(模拟器)和Marshmallow(手机)中运行良好,但每次单击按钮时都会在Jelly Bean(模拟器和手机)上崩溃,并显示错误对话框“不幸[App Name]已停止”。

有趣的是,使用“Button”代替AppCompatButton或使用onClickListener解决了这个问题。有谁知道为什么会这样?

发生错误后,堆栈跟踪(来自Android Monitor的输出):

06-01 11:55:16.352 3480-3480/mypackage.khateratebook E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          java.lang.IllegalStateException: Could not find a method showTOC(View) in the activity class android.support.v7.widget.TintContextWrapper for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'btn_show_toc_id'
                                                                              at android.view.View$1.onClick(View.java:3586)
                                                                              at android.view.View.performClick(View.java:4204)
                                                                              at android.view.View$PerformClick.run(View.java:17355)
                                                                              at android.os.Handler.handleCallback(Handler.java:725)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                              at android.os.Looper.loop(Looper.java:137)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                              at java.lang.reflect.Method.invokeNative(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:511)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                              at dalvik.system.NativeStart.main(Native Method)
                                                                           Caused by: java.lang.NoSuchMethodException: showTOC [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:3579)
                                                                              at android.view.View.performClick(View.java:4204) 
                                                                              at android.view.View$PerformClick.run(View.java:17355) 
                                                                              at android.os.Handler.handleCallback(Handler.java:725) 
                                                                              at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                              at android.os.Looper.loop(Looper.java:137) 
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                              at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                              at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                              at dalvik.system.NativeStart.main(Native Method) 

styles.xml的内容:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

    </style>
    <style name="BookTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

我使用BookTheme风格的唯一地方是我的第三个活动,即以下列方式的activity_main2.xml:

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

    <WebView
        android:id="@+id/webview_id"
        android:layout_width="match_parent"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="100dp"
        android:theme="@style/BookTheme"
        android:textDirection="rtl"


        />


</RelativeLayout>

0 个答案:

没有答案