I'm making a simple quiz app that consist of 10 questions with 10sec countdown timer each questions, which means i have 10 activity for questions. The scoring works while each questions answer correctly multiply by remaining time on CountDownTimer method, so it will be: score = answer * timeLeft; And the total score will be printed on ResultActivity at the end of activity after activity 10. The problem is i cannot pass my score variable through each activity and when i click next button that intent from activity 10 to ResultActivity, the ResultActivity cannot be open or force close. Here is my code:
TestActivity1
package com.finalproject.logicaltest;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.Timer;
import butterknife.ButterKnife;
import butterknife.Bind;
import static android.R.id.message;
import static android.R.string.cancel;
import static com.finalproject.logicaltest.R.id.rb1;
import static com.finalproject.logicaltest.R.id.rb2;
import static com.finalproject.logicaltest.R.id.rb3;
import static com.finalproject.logicaltest.R.id.rb4;
public class TestActivity1 extends AppCompatActivity {
@Bind(R.id.rb1) RadioButton rB1;
@Bind(R.id.rb2) RadioButton rB2;
@Bind(R.id.rb3) RadioButton rB3;
@Bind(R.id.rb4) RadioButton rB4;
@Bind(R.id.next) Button bNext;
@Bind(R.id.timer) TextView cDown;
public int answer = 0;
public int score = 0;
public long timeLeft = 0;
//The number of milliseconds in the future from
//the call to start() until the countdown is done
public long millisInFuture = 11000; //11 seconds
//The interval along the way to receive onTick(long) callbacks
long countDownInterval = 1000; //1 second
public long millisUntilFinished;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
ButterKnife.bind(this);
setTimer();
}
public void setTimer() {
//Initialize a new CountDownTimer instance
final CountDownTimer timer = new CountDownTimer(millisInFuture,countDownInterval){
public void onTick(long millisUntilFinished){
//do something in every tick
//Display the remaining seconds to app interface
//1 second = 1000 milliseconds
cDown.setText("" + millisUntilFinished / 1000);
timeLeft = millisUntilFinished / 1000;
}
public void onFinish(){
//Do something when count down finished
cDown.setText("NEXT!");
Intent intent = new Intent(TestActivity1.this,TestActivity2.class);
intent.putExtra("score", score);
startActivity(intent);
finish();
timeLeft = 0;
}
}.start();
bNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
timeLeft = millisUntilFinished;
Intent intent = new Intent(TestActivity1.this,TestActivity2.class);
intent.putExtra("score", score);
startActivity(intent);
finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
}
public void onRadioButtonClicked (View v) {
boolean checked = ((RadioButton) v).isChecked();
switch (v.getId()) {
case rb1:
if (checked){
answer++;
break;
}
case rb2:
if (checked){
answer = 0;
break;
}
case rb3:
if (checked){
answer = 0;
break;
}
case rb4:
if (checked){
answer = 0;
break;
}
}
score = ((int)(timeLeft) * Integer.valueOf(answer));
}
}
It pass score with putExtra through TestActivity2 until TestActivity10 like this:
package com.finalproject.logicaltest;
/**
* Created by VICKY on 19-May-17.
*/
import android.app.Activity;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.Bind;
import static com.finalproject.logicaltest.R.id.rb1;
import static com.finalproject.logicaltest.R.id.rb2;
import static com.finalproject.logicaltest.R.id.rb3;
import static com.finalproject.logicaltest.R.id.rb4;
import static com.finalproject.logicaltest.R.id.rb5;
public class TestActivity10 extends AppCompatActivity {
@Bind(rb1) RadioButton rB1;
@Bind(rb2) RadioButton rB2;
@Bind(rb3) RadioButton rB3;
@Bind(rb4) RadioButton rB4;
@Bind(rb5) RadioButton rB5;
@Bind(R.id.end) Button bEnd;
@Bind(R.id.timer) TextView cDown;
public int answer = 0;
public int score = 0;
public long timeLeft = 0;
//The number of milliseconds in the future from
//the call to start() until the countdown is done
public long millisInFuture = 11000; //11 seconds
//The interval along the way to receive onTick(long) callbacks
long countDownInterval = 1000; //1 second
public long millisUntilFinished;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test10);
ButterKnife.bind(this);
score = getIntent().getExtras().getInt("score");
setTimer();
}
public void setTimer() {
//Initialize a new CountDownTimer instance
final CountDownTimer timer = new CountDownTimer(millisInFuture,countDownInterval){
public void onTick(long millisUntilFinished){
//do something in every tick
//Display the remaining seconds to app interface
//1 second = 1000 milliseconds
cDown.setText("" + millisUntilFinished / 1000);
timeLeft = millisUntilFinished / 1000;
}
public void onFinish(){
//Do something when count down finished
cDown.setText("NEXT!");
Intent intent = new Intent(TestActivity10.this,ResultActivity.class);
intent.putExtra("score", score);
startActivity(intent);
finish();
timeLeft = 0;
}
}.start();
bEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
timeLeft = millisUntilFinished;
Intent intent = new Intent(TestActivity10.this,ResultActivity.class);
intent.putExtra("score", score);
startActivity(intent);
finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
});
}
public void onRadioButtonClicked (View v) {
boolean checked = ((RadioButton) v).isChecked();
switch (v.getId()) {
case rb1:
if (checked){
answer = 0;
break;
}
case rb2:
if (checked){
answer++;
break;
}
case rb3:
if (checked){
answer = 0;
break;
}
case rb4:
if (checked){
answer = 0;
break;
}
case rb5:
if (checked){
answer = 0;
break;
}
}
score += ((int)(timeLeft) * Integer.valueOf(answer));
}
}
And printed total score on ResultActivity:
package com.finalproject.logicaltest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import static android.R.id.message;
public class ResultActivity extends AppCompatActivity {
public int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
score = getIntent().getExtras().getInt("score");
TextView result = (TextView) findViewById(R.id.total_score);
result.setText(score);
}
}
What's wrong with my code?