Java 按下按钮时应用程序崩溃
我在Android studio中创建了一个由4个问题组成的小测验。一个问题有单选按钮,第二个有复选框,第三个有常规按钮,第四/第五个是编辑文本。每当我在运行后按下任何按钮,应用程序就会崩溃,日志中就会显示一个巨大的错误 主要活动:Java 按下按钮时应用程序崩溃,java,android,android-studio,compiler-errors,Java,Android,Android Studio,Compiler Errors,我在Android studio中创建了一个由4个问题组成的小测验。一个问题有单选按钮,第二个有复选框,第三个有常规按钮,第四/第五个是编辑文本。每当我在运行后按下任何按钮,应用程序就会崩溃,日志中就会显示一个巨大的错误 主要活动: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hende.germanquiz.MainActivity">
<TextView
android:id="@+id/Header_text_view"
style="@style/Header"
android:text="@string/header_text" />
<TextView
android:id="@+id/question_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="@string/questionOne" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true"
android:onClick="Bitte schön"
android:text="@string/wrong1_answer" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="Ich heiße"
android:text="@string/correct_answer" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="Sprechen sie Deutsch?"
android:text="@string/wrong2_answer" />
<TextView
android:id="@+id/question_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="@string/questionTwo"
android:textColor="#B71C1C" />
<CheckBox
android:id="@+id/checkbox1"
style="@style/Buttons"
android:onClick="rechnung1"
android:text="@string/correct2_answer" />
<CheckBox
android:id="@+id/checkbox2"
style="@style/Buttons"
android:onClick="rechnung2"
android:text="@string/correct3_answer" />
<CheckBox
android:id="@+id/checkbox3"
style="@style/Buttons"
android:onClick="Vogel"
android:text="Hast du einen Vogel?" />
<TextView
android:id="@+id/question_three_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="@string/question_three" />
<TextView
android:id="@+id/question_threea_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/question_threea" />
<Button
android:id="@+id/button2"
style="@style/RegularButton"
android:text="@string/wrong4_answer"
android:onClick="button2"/>
<Button
android:id="@+id/button3"
style="@style/RegularButton"
android:text="@string/wrong5_answer"
android:onClick="button3"/>
<Button
android:id="@+id/button1"
style="@style/RegularButton"
android:text="@string/correct4_answer"
android:onClick="button1"/>
<TextView
android:id="@+id/aurora_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/questionFour"
android:layout_marginTop="10dp" />
<EditText
android:id="@+id/edit_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/questionFive"
android:layout_marginTop="10dp"/>
<EditText
android:id="@+id/edit2_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit_button"
android:onClick="calculateScore"/>
<TextView
android:id="@+id/score_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</LinearLayout>
</ScrollView>
日志错误:
08-27 13:07:50.408 3344-3344/com.example.hende.germanquiz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hende.germanquiz, PID: 3344
java.lang.IllegalStateException: Could not find method rechnung2(View) in a parent or ancestor Context for
android:onClick attribute defined on view class android.support.v7.widget.AppCompatCheckBox with id 'checkbox2'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5609)
at android.widget.CompoundButton.performClick(CompoundButton.java:122)
at android.view.View$PerformClick.run(View.java:22259)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
08-27 12:49:22.499 32531-416/com.example.hende.germanquiz E/InstantRun:
Failed to update existing theme for activity com.example.hende.germanquiz.MainActivity@587f4d8
java.lang.NoSuchFieldException: No field mTheme in class Landroid/content/res/Resources$Theme;
(declaration of 'android.content.res.Resources$Theme' appears in /system/framework/framework.jar)
at java.lang.Class.getDeclaredField(Native Method)
at com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources(MonkeyPatcher.java:374)
at com.android.tools.fd.runtime.Server.restart(Server.java:523)
at com.android.tools.fd.runtime.Server.access$600(Server.java:61)
at com.android.tools.fd.runtime.Server$SocketServerReplyThread.handle(Server.java:318)
at com.android.tools.fd.runtime.Server$SocketServerReplyThread.run(Server.java:190)
at com.android.tools.fd.runtime.Server$SocketServerThread.run(Server.java:158)
at java.lang.Thread.run
08-27 12:49:43.382 32531-32531/com.example.hende.germanquiz E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-27 12:49:43.590 32531-32531/com.example.hende.germanquiz E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
在xml中定义属性onClick。这意味着当您单击该按钮时,android将调用“onClick”属性中命名的方法。由于MainActivity中没有定义这些方法,因此会出现运行时错误,这里有多个问题。 第一个原因似乎是您在复选框的onClick属性上声明了一个名为“rechnung2”的方法,但从未实现过它。 在第二个错误中,您使用的主题与默认主题不同,并且可能在链的某个位置导致冲突。
跟踪中的最后一个错误可能是因为您在
活动中的显示方法中将分数添加到空白处
没有名为rechnung2的方法
你的logcat说:
java.lang.IllegalStateException:在android的父或祖先上下文中找不到方法rechnung2(视图):在视图类android.support.v7.widget.AppCompatCheckBox上定义的onClick属性,id为“checkbox2”
建议:请检查您的xml并找到所有android:onclick
元素,然后检查您的活动
,在您的活动
中,您必须具备的方法
另外,Android Studio
会在您的活动中没有该方法时发出警告,例如:
单击onClick
XML属性调用一个方法,您的main活动中必须有该方法。此外,您还必须遵守以下规则:
The method you declare in the android:onClick attribute must have a
specific signature. Specifically, the method must:
Be public
Return void
Define a View as its only parameter (this will be the View that was clicked)
您应该阅读android的文档:onClick,您没有正确使用它。a/阅读您的错误。b/您认为android:onClick=“rechnung2”
有什么作用?对于onClick事件,为什么不在onCreate方法中设置onclicklisteners并将其注册到全局处理程序。您可以使用switch语句适当地处理每个语句。见:
The method you declare in the android:onClick attribute must have a
specific signature. Specifically, the method must:
Be public
Return void
Define a View as its only parameter (this will be the View that was clicked)