我制作了一个简单的测验应用程序,其中包含10个问题,每个问题有10秒倒数计时器,这意味着我有10个问题活动。评分有效,而每个问题的答案正确地乘以CountDownTimer方法的剩余时间,因此它将是:score = answer * timeLeft;在活动10之后活动结束时,总得分将打印在ResultActivity上。问题是我无法通过每个活动传递得分变量,当我单击从活动10到ResultActivity的意图的下一个按钮时,ResultActivity无法打开或强制关闭。这是我的代码:
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));
}
}
它通过TestActivity2通过putExtra传递得分,直到TestActivity10像这样:
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));
}
}
并在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);
}
}
我的代码出了什么问题?
答案 0 :(得分:0)
你可以使用应用程序类,在Application类中保存得分值,可以从任何你想要的类访问。(因为你只需要保存得分值。)你可以获得更多信息检查here
希望有所帮助......
答案 1 :(得分:0)
你好试试这个代码可能有所帮助,根据你的逻辑使用
声明和初始化
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("results", Context.MODE_PRIVATE);
存储您在其他活动中使用的分数
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("score", score);
editor.commit();
检索存储的数据
SharedPreferences prefs = this.getSharedPreferences("results", Context.MODE_PRIVATE);
String lanSettings = prefs.getString("score", null);
答案 2 :(得分:0)
IMO 您将score
设置为TextView,这是不正确的。
设置如下:
result.setText(String.valueOf(score));
或者像这样:
result.setText(" "+score);
看看是否有效。