如何修复java代码以将结果显示为name和score
我已经为4个问题提供了xml和java代码。我使用它,每个问题都有4个单选按钮,在javam中,如果用户选择了正确的答案,它必须在最后添加分数。结果必须显示姓名和分数,但我似乎不能从那里继续下去如何修复java代码以将结果显示为name和score,java,android,methods,Java,Android,Methods,我已经为4个问题提供了xml和java代码。我使用它,每个问题都有4个单选按钮,在javam中,如果用户选择了正确的答案,它必须在最后添加分数。结果必须显示姓名和分数,但我似乎不能从那里继续下去 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFF00" android:orientati
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFF00"
android:orientation="vertical">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:inputType="textPersonName"
android:text="@string/name"
android:importantForAutofill="no"
android:hint="@string/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="@string/Question1"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="@string/firstQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/GQ11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ11" />
<RadioButton
android:id="@+id/GQ12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ12" />
<RadioButton
android:id="@+id/GQ13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ13" />
<RadioButton
android:id="@+id/GQ14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ14" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="@string/Questuin2"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="@string/secondQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/GQ21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ21" />
<RadioButton
android:id="@+id/GQ22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ22" />
<RadioButton
android:id="@+id/GQ23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ23" />
<RadioButton
android:id="@+id/GQ24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ24" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="@string/Question3"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="@string/thirdQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/GQ31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ31" />
<RadioButton
android:id="@+id/GQ32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ32" />
<RadioButton
android:id="@+id/GQ33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ33" />
<RadioButton
android:id="@+id/GQ34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ34" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="22dp"
android:text="@string/Question4"
android:textAllCaps="true"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="20dp"
android:text="@string/fourthQuestion"
android:textSize="16sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/GQ41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ41" />
<RadioButton
android:id="@+id/GQ42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ42" />
<RadioButton
android:id="@+id/GQ43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ43" />
<RadioButton
android:id="@+id/GQ44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="@string/GQ44" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/end"
android:textSize="16sp"
android:padding="15dp"
android:layout_marginTop="22dp"
android:id="@+id/submit"
android:onClick="result"/>
</LinearLayout>
当我按下“提交”按钮时,应用程序崩溃。您的应用程序崩溃,因为您没有OnClick侦听器。你需要一个来处理按钮点击。它应该是这样的:
Button submitButton = (Button) findViewById(R.id.submit);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO
}
});
在onClick方法中,您希望在单击submit按钮后对答案和任何您希望发生的事情执行检查。此外,由于您有一个较长的线性布局,我建议您添加一个滚动视图。寻求调试帮助的问题(“为什么此代码不工作?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现此问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。您的代码太长。首先尝试制作一个能正常工作的较短版本。另外,如果你说的是“应用程序崩溃”-你也应该说它是如何崩溃的,否则我们无法帮助。当我按下提交按钮时,应用程序崩溃的结果必须与名称一样。。。。。。分数…谢谢你的评论我有滚动查看第一个问题分数没有出现和名称在first@kamal对于问题1到4和结果,您有函数,但您从未调用这些函数来检查答案。在onClick函数中,您希望调用这些函数来检查和添加分数,然后调用result函数来制作toast。你应该首先检查你的函数,因为它们有一些错误的逻辑。例如,如果答案错误,如果你没有加分或减分,那么你的
else if
子句是多余的。您可能还应该使用这些函数的局部变量,并返回该变量,而不是使用score
变量。
Button submitButton = (Button) findViewById(R.id.submit);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO
}
});