2017-12-26 7 views
0

私のアプリは時限付きの数学ゲームです。タイマーがなくなる前にできるだけ多くの質問に答えてください。時間がなくなると、GameOverActivityは現在のアクティビティになります。私は答えが無ければ、アプリがクラッシュすることに気づいた。私が少なくとも1つの答えを与えると、アプリはクラッシュせず、すべて正常です。私のコードのどこに欠陥が存在するかわからない。答えが返ってこないと、アプリケーションがクラッシュする

これは、主な活動

ある
package stormy.incremental.randomtest; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import java.util.Random; 

public class FastMathActivity extends AppCompatActivity { 

    int rand1, rand2, randDecider, correctAnswer, falseAnswer, problemsSolved; 
    String response,sumStr; 
    MyCountDownTimer myCountDownTimer; 

    int score; 

    Random r; 
    TextView randTV1, randTV2, scoreTV, sumTV, problemsSolvedTV, timerTV; 
    Button choice1, choice2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_random_test); 
     problemsSolved =0; 
     falseAnswer = 1; 

//Initializing TextViews 

     timerTV = ((TextView) findViewById(R.id.timer)); 

     randTV1 = ((TextView) findViewById(R.id.rand1)); 
     randTV2 = ((TextView) findViewById(R.id.rand2)); 
     sumTV = ((TextView) findViewById(R.id.sum)); 
     scoreTV = ((TextView) findViewById(R.id.score)); 
     problemsSolvedTV = ((TextView) findViewById(R.id.problemsSolved)); 

     choice1 = ((Button) findViewById(R.id.choice1)); 
     choice2 = ((Button) findViewById(R.id.choice2)); 

//Initializing a Random 
     r = new Random(); 

//Set the first question 
     setRandomProblem(); 

//Starting the timer 
     myCountDownTimer = new MyCountDownTimer(timerTV, 5000, 1000); 
     myCountDownTimer.start(); 

// Button Listeners 
     choice1.setOnClickListener(new Button.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       checkResponse((Button)v); 
       setRandomProblem(); 

      } 
     }); 
     choice2.setOnClickListener(new Button.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       checkResponse((Button)v); 
       setRandomProblem(); 

      } 
     }); 
    } 
    public void checkResponse(Button v) { 


//Convert the response and correctAnswer to String in order to compare values 
     response = v.getText().toString(); 
     sumStr = Integer.toString(correctAnswer); 

//If the user clicks the correct answer, increment score 
     if ((response.equals(sumStr))) { 
      score++; 
      scoreTV.setText(score+""); 
      } 

//Increment the total amount of problems solved 

     problemsSolved++; 
     problemsSolvedTV.setText(problemsSolved+""); 

//Keep track of the score within the timer 
     myCountDownTimer.recordScore(score,problemsSolved); 
    } 

    private void setRandomProblem() { 

//Assigning random values to ints 
     rand1 = r.nextInt(5 - 1) + 1; 
     rand2 = r.nextInt(5 - 1) + 1; 
     randDecider = r.nextInt(2) + 1; 

//The correctAnswer of the randoms 
     correctAnswer = rand1 + rand2; 

//Setting the texts of the random values 
     randTV1.setText(rand1 + ""); 
     randTV2.setText(rand2 + ""); 

//If the random deciding number is 1, set answer on choice1 
     if (randDecider == 1) { 
      choice1.setText(correctAnswer + ""); 
      choice2.setText(correctAnswer + falseAnswer + ""); 
     } 
     //If the random deciding number is 2, set answer on choice2 
     else { 
      choice1.setText(correctAnswer + falseAnswer + ""); 
      choice2.setText(correctAnswer + ""); 
     } 

    } 
    @Override 
    public void onStop(){ 
     super.onStop(); 
//Stop the timer 
     myCountDownTimer.cancel(); 
    } 

    } 

これは、これはタイマーであるGameOverActivity

package stormy.incremental.randomtest; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.TextView; 

/** 
* Created by kamalu on 12/25/2017. 
*/ 

public class GameOverActivity extends AppCompatActivity { 
    TextView scoreTV, problemsSolvedTV, percentageTV; 
    int score, problemsSolved, percentage; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gameover); 

//Initializing TextViews 
     scoreTV = ((TextView)findViewById(R.id.score)); 
     problemsSolvedTV = ((TextView)findViewById(R.id.problemsSolved)); 
     percentageTV = ((TextView)findViewById(R.id.percentage)); 

//Opening Bundle and assigning values 
     Bundle extras = getIntent().getExtras(); 
     score = extras.getInt("score"); 
     problemsSolved = extras.getInt("problemsSolved"); 

//calculating the accuracy 
     percentage = (score/problemsSolved)*100; 

//Displaying the score 
     percentageTV.setText(percentage+""); 
     scoreTV.setText(score+""); 
     problemsSolvedTV.setText(problemsSolved+""); 
    } 

//Start the game over 
    public void retry(View v){ 
     Intent retryIntent = new Intent(GameOverActivity.this, FastMathActivity.class); 
     startActivity(retryIntent); 
    } 
    public void onBackPressed() 
    { 

    } 
} 

です。このクラスのonFinish()メソッドがGameOverActivityを開始することに注意することが重要であると考えています。

package stormy.incremental.randomtest; 

import android.content.Intent; 
import android.os.CountDownTimer; 
import android.widget.TextView; 


public class MyCountDownTimer extends CountDownTimer { 

TextView textCounter; 
int score,problemsSolved; 

public MyCountDownTimer(TextView textCounter, long millisInFuture, long countDownInterval) { 
    super(millisInFuture, countDownInterval); 
    this.textCounter = textCounter; 

} 

@Override 
public void onTick (long millisUntilFinished){ 

    textCounter.setText(String.valueOf(millisUntilFinished/1000)); 
} 

@Override 
public void onFinish() { 
     Intent gameOverIntent = new Intent(textCounter.getContext(), GameOverActivity.class); 
     gameOverIntent.putExtra("score", score); 
     gameOverIntent.putExtra("problemsSolved", problemsSolved); 
     textCounter.getContext().startActivity(gameOverIntent); 

} 

//Keep track of the scores 
    public void recordScore(int score,int problemsSolved){ 
    this.problemsSolved = problemsSolved; 
    this.score = score; 
    } 

} 
+0

どのようなエラーでクラッシュしますか? – Carcigenicate

+0

あなたは私たちに多くのコードを与えてくれました。少なくとも、エラーの原因となっているコード行を指摘できたら助かります。 SOのほとんどの人は、すべてを読んで忍耐を持っていません。 –

+0

@Carcigenicate java.lang.ArithmeticException:ゼロで除算します。これは、答えが与えられなければ、変数は0になり、Javaは前進できないことを意味します。どのように私はこれをmanueverできますか? –

答えて

1

あなたがチェックする必要があります:

//calculating the accuracy 
percentage = (score/problemsSolved)*100; 

をproblemsSolved = 0の場合、あなたのアプリがexeptionsでクラッシュします:java.lang.ArithmeticExceptionあなたが参照することができ :

if (problemSolved != 0){ 
    //calculating the accuracy 
    percentage = (score/problemsSolved)*100; 
} else { 
    // handle with problemSolved = 0; 
} 

私はそれはあなたの問題を助けることを願って!

関連する問題