需要用我的小測驗數學幫助......寫答案崩潰
當我運行仿真器的應用程序,它的的EditText(answerTxt)點擊時崩潰。
我想在editTextbox中回答數學問題,在做完這些之後,我想單擊按鈕(button1)以查看它是否是正確的答案。如果是對的,我會得到1分。如果我回答這個問題,不管答案是對還是錯,都會有一個新的問題。
package com.ebookfrenzy.mattespelet;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Random;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.EditText;
public class PlayActivity extends AppCompatActivity implements OnClickListener {
private int level = 0, answer = 0, operator = 0, operand1 = 0, operand2 = 0;
private final int ADD_OPERATOR = 0, SUBTRACT_OPERATOR = 1, MULTIPLY_OPERATOR = 2, DIVIDE_OPERATOR = 3;
private String[] operators = {"+", "-", "x", "/"};
private int[][] levelMin = {
{1, 11, 21},
{1, 5, 10},
{2, 5, 10},
{2, 3, 5}};
private int[][] levelMax = {
{10, 25, 50},
{10, 20, 30},
{5, 10, 15},
{10, 50, 100}};
private Random random;
private EditText answerTxt;
private TextView question, scoreTxt;
private Button enterBtn;
private ImageView response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
question = (TextView)findViewById(R.id.textView2);
answerTxt = (EditText) findViewById(R.id.answertxt);
enterBtn = (Button)findViewById(R.id.button1);
scoreTxt = (TextView)findViewById(R.id.score);
response = (ImageView)findViewById(R.id.response);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
int passedLevel = extras.getInt("level", -1);
if(passedLevel>=0) level = passedLevel;
}
random = new Random();
chooseQuestion();
}
private void chooseQuestion(){
answerTxt.setText("= ?");
operator = random.nextInt(operators.length);
operand1 = getOperand();
operand2 = getOperand();
if(operator == SUBTRACT_OPERATOR){
while(operand2>operand1){
operand1 = getOperand();
operand2 = getOperand();
}
}
else if(operator==DIVIDE_OPERATOR){
while((((double)operand1/(double)operand2)%1 > 0) || (operand1==operand2))
{
operand1 = getOperand();
operand2 = getOperand();
}
}
switch(operator)
{
case ADD_OPERATOR:
answer = operand1+operand2;
break;
case SUBTRACT_OPERATOR:
answer = operand1-operand2;
break;
case MULTIPLY_OPERATOR:
answer = operand1*operand2;
break;
case DIVIDE_OPERATOR:
answer = operand1/operand2;
break;
default:
break;
}
question.setText(operand1+" "+operators[operator]+" "+operand2);
}
private int getOperand(){
return random.nextInt(levelMax[operator][level] - levelMin[operator][level] + 1)
+ levelMin[operator][level];
}
@Override
public void onClick(View view)
{
if(view.getId()==R.id.button1) {
String answerContent = answerTxt.getText().toString();
if(!answerContent.endsWith("?"))
{
int enteredAnswer = Integer.parseInt(answerContent.substring(2));
int exScore = getScore();
if(enteredAnswer==answer){
scoreTxt.setText("Score: "+(exScore+1));
response.setImageResource(R.drawable.tick);
response.setVisibility(View.VISIBLE);
//correct
}
}
else {
scoreTxt.setText("Score: 0");
response.setImageResource(R.drawable.cross);
response.setVisibility(View.VISIBLE);
//incorrect
}
chooseQuestion();
}
else {
int number=Integer.valueOf(answerTxt.getText().toString()); ;
if(answerTxt.getText().toString().endsWith("?"))
answerTxt.setText("= "+number);
else
answerTxt.append(""+number);
}
response.setVisibility(View.INVISIBLE);
}
private int getScore(){
String scoreStr = scoreTxt.getText().toString();
return Integer.parseInt(scoreStr.substring(scoreStr.lastIndexOf(" ")+1));
}
}
這裏是錯誤...
10-13 20:39:05.993 4365-4399/com.ebookfrenzy.mattespelet E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa9d15de0
10-13 20:39:06.974 4365-4399/com.ebookfrenzy.mattespelet E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4056bb0
10-13 20:39:07.885 4365-4365/com.ebookfrenzy.mattespelet D/Something: = ?
10-13 20:39:07.886 4365-4365/com.ebookfrenzy.mattespelet D/AndroidRuntime: Shutting down VM
10-13 20:39:07.886 4365-4365/com.ebookfrenzy.mattespelet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ebookfrenzy.mattespelet, PID: 4365
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid int: "= ?"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at java.lang.Integer.valueOf(Integer.java:525)
at com.ebookfrenzy.mattespelet.PlayActivity.onClick(PlayActivity.java:167)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
這是XML文件..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.ebookfrenzy.mattespelet.PlayActivity"
android:background="#815959">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textSize="30sp"
android:layout_alignBottom="@+id/answertxt"
android:width="200dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Räkna ihop"
android:id="@+id/textView5"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:background="#fcfafa" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kontrollera_button"
android:id="@+id/button1"
android:layout_marginBottom="79dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/answertxt"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView5"
android:layout_toEndOf="@+id/textView5"
android:onClick="onClick"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Score: 0"
android:id="@+id/score"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/answertxt"
android:layout_alignEnd="@+id/answertxt"
android:textSize="20sp" />
<ImageView
android:id="@+id/response"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_weight="1"
android:contentDescription="result"
android:src="@drawable/tick"
/>
</LinearLayout>
</RelativeLayout>
'由於:java.lang.NumberFormatException:無效的int:「=?」'。我不確定爲什麼,但在答案框中有等號和問號。我會把它們放在他們自己的盒子裏,或者將它們過濾出來,然後用'try/catch'包裝,並且如果它捕獲到一個'NumberFormatException'則執行一些操作。 – codeMagic
由這行PlayActivity.java:167引起的錯誤。檢查你的PlayActivity行167. –
第167行是: int number = Integer.valueOf(answerTxt.getText()。toString()); ; 不知道用什麼來代替它 – soffman