Java 将可变分数从一项活动传递到另一项活动

Java 将可变分数从一项活动传递到另一项活动,java,android,Java,Android,我正在制作一个简单的测验应用程序,由10个问题组成,每个问题有10秒的倒计时,这意味着我有10个问题活动。当每个问题的答案正确乘以倒数计时方法的剩余时间时,计分有效,因此它将是:分数=答案*时间间隔;活动10结束后,总分将在活动结束时打印在ResultActivity上。问题是我无法通过每个活动传递我的分数变量,当我单击“下一步”按钮,从活动10到ResultActivity,ResultActivity无法打开或强制关闭。这是我的密码: 测试性1 package com.finalprojec

我正在制作一个简单的测验应用程序,由10个问题组成,每个问题有10秒的倒计时,这意味着我有10个问题活动。当每个问题的答案正确乘以倒数计时方法的剩余时间时,计分有效,因此它将是:分数=答案*时间间隔;活动10结束后,总分将在活动结束时打印在ResultActivity上。问题是我无法通过每个活动传递我的分数变量,当我单击“下一步”按钮,从活动10到ResultActivity,ResultActivity无法打开或强制关闭。这是我的密码:

测试性1

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));

    }




}
它通过putExtra通过TestActivity2通过分数,直到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));

    }




}
result.setText(String.valueOf(score));
result.setText(" "+score);
并打印结果性总分:

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);
    }
}

我的代码出了什么问题?

您可以使用应用程序类,将分数值保存在应用程序类中,该类可以从您想要的任何类访问。(因为您只需要保存分数值。)有关更多信息,请查看


希望对您有所帮助。

您好,请尝试此代码,它可能会有所帮助,请根据您的逻辑使用

声明和初始化

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);
检索存储的数据

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);

IMO您将
分数设置为您的文本视图,这是不正确的。
或者这样设置:

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));

    }




}
result.setText(String.valueOf(score));
result.setText(" "+score);
或者像这样:

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));

    }




}
result.setText(String.valueOf(score));
result.setText(" "+score);

看看这是否有效。

您可以将结果值存储在共享首选项中,并在结果屏幕中设置它,而不是总是传递它。为什么要为每个问题创建活动?你应该使用Fragment并在你有问题时重复使用它。
我正在制作一个简单的测验应用程序,由10个问题组成,每个问题有10秒倒计时,这意味着我有10个问题活动
这似乎是你试图以错误的方式解决问题。我想你可以只做一个普通的活动,接受一个问题,然后展示这个问题,而不是为每个问题创建一个新的活动。现在你们所有的10项活动基本上都是彼此的复制品。随着时间的推移,维护这些会变得越来越困难。不工作是什么意思?分数返回0或您得到异常或…?检查您的Android监视器一次是否有异常。虽然这是一个有效的解决方案,但通常不建议这样做。
应用程序
类为什么要保存这些数据?最好通过意向和额外的东西来传递。你只需要分数值。。。。对于该应用程序,类可以工作。毫无疑问,意图是它应该工作的好方法,但应用程序类在这里也可以工作,请查看我提到的链接。我的观点是,转储应用程序类中的所有内容会很快导致一个紧密耦合的系统,它会变成一个怪物,无法对其进行任何形式的测试/重构。我应该在哪里编写此代码?在一次创建中?公共课?或者哪种方法?在开始测试之前,你必须先查看SharedReferences,onCreate可以在这里正常工作我只需要添加这些代码或者用我的其他错误代码替换它?我应该换哪一个?在声明中,与我的不同,您没有声明像
public int score=0这样的全局变量或它将自动工作?我应该让putExtra()写在那里还是用SharedReferences替换它?你应该使用shared preference而不是发送intentEdit.putInt(“userScore1”,零);int userScore1=prefs.getInt(“userScore1”,0);编辑:putFloat(“percange”,1.2f);float someFloat=prefs.getFloat(“percantage”,0.0f);