私のアプリケーションには、クリックすると別のアクティビティを開始するはずのボタンがあります。次のようにボタンへのonClick属性::私はアンドロイドを追加したなぜアンドロイド:onClick属性がAndroid 4.2でクラッシュするのですか?
<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);
}
これは、ヌガー(エミュレータ)とマシュマロ(電話)で正常に動作しますが、ボタンがクリックされる度にJelly Bean(エミュレータと電話)でクラッシュし、「残念ながら[App Name]が停止しました」というエラーダイアログが表示されます。
興味深いことに、AppCompatButtonの代わりに "Button"を使うか、onClickListenerを使って問題を解決します。誰がなぜこれが当てはまるのか知っていますか?
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スタイルがある私が使用している唯一の場所。ここ
は、スタックトレースを(Androidのモニターからの出力)エラーが発生した後に行きます私の3番目のアクティビティ、つまり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>
それをチェックし、ここでスタックトレースを入れ –
@LuizFernandoSalvaterraのスタックトレースが追加されました。 – JasonStack
これはおそらくスタイルの問題です。ここにstyles.xmlも入れてください。 –