Java 主活动上的数据无法使用SharedReferences正确保存/加载

Java 主活动上的数据无法使用SharedReferences正确保存/加载,java,android,sharedpreferences,Java,Android,Sharedpreferences,这是一个测验应用程序,我正在工作,与Android Studio。。。我正在尝试使用SharedReferences,使用putInt和getInt方法将每个测验的分数值放入变量中,存储在字符串名称下,然后onCreate的一部分是使用putInt方法中引用的字符串名称获取Int。。。当用户首次打开应用程序时,主活动应加载所有0。。。当用户完成测验时,分数被传递回主要活动,并且分数应该保持在那里。。。无论用户是否继续、转到另一个屏幕、另一个应用程序或完全关闭该应用程序,每次测验的分数都应保持不变

这是一个测验应用程序,我正在工作,与Android Studio。。。我正在尝试使用SharedReferences,使用putInt和getInt方法将每个测验的分数值放入变量中,存储在字符串名称下,然后onCreate的一部分是使用putInt方法中引用的字符串名称获取Int。。。当用户首次打开应用程序时,主活动应加载所有0。。。当用户完成测验时,分数被传递回主要活动,并且分数应该保持在那里。。。无论用户是否继续、转到另一个屏幕、另一个应用程序或完全关闭该应用程序,每次测验的分数都应保持不变,但不会:

当前,主活动上的每个测验按钮都会关闭主活动完成,每个测验的最后一项活动称为主要活动并通过得分。。。我还让每个测验按钮在主活动关闭前调用onStop方法,onStop方法使用SharedReferences.Editor和putInt方法将每个点值的值放入主活动。。。然后我在onCreate的开始部分使用了SharedReference和getInt方法

所以基本上,我想做的是,每次用户点击一个测验按钮,每次测验的分数都应该保存,使用putin方法,使用字符串名称,以及我用于每次测验分数的实际int变量,当用户完成测验后返回主活动时,用户已经得分的任何分数都应该通过getInt方法自动加载。。。有关我的程序中的一些编码,请参见下图。。。如果有人需要查看更多代码,请告诉我,我希望有人能帮助我!:

我试过以前的人的建议,但仍然不起作用。。。请参阅下面我的部分代码,并让我知道需要修复的内容:

//Get points from Quiz One
        Bundle getFromQuizOne = getIntent().getExtras();
        if(getFromQuizOne != null)
        {
            //Get points from Quiz One
            numberQuizOnePoints = getFromQuizOne.getInt("PointsFromAllQuizOne");

            SharedPreferences shareInt = getApplicationContext().getSharedPreferences(QUIZ, MODE_PRIVATE);
            SharedPreferences.Editor editInt = shareInt.edit();

            //Save the value of points from Quiz One with SharedPreferences
            editInt.putInt("quizOnePointsYeah", numberQuizOnePoints);
            editInt.commit();

            //Get the value of points from all other quizzes with getInt from SharedPreferences
            numberQuizTwoPoints = shareInt.getInt("quizTwoPointsYeah", 0);
            numberQuizThreePoints = shareInt.getInt("quizThreePointsYeah", 0);
            numberQuizFourPoints = shareInt.getInt("quizFourPointsYeah", 0);
            numberQuizFivePoints = shareInt.getInt("quizFivePointsYeah", 0);
            numberQuizSixPoints = shareInt.getInt("quizSixPointsYeah", 0);
            numberQuizSevenPoints = shareInt.getInt("quizSevenPointsYeah", 0);
            numberQuizEightPoints = shareInt.getInt("quizEightPointsYeah", 0);
            numberQuizNinePoints = shareInt.getInt("quizNinePointsYeah", 0);
            numberQuizTenPoints = shareInt.getInt("quizTenPointsYeah", 0);

            //Sum up points from each quiz, and put sum in totalPoints variable
            totalPoints = numberQuizOnePoints + numberQuizTwoPoints + numberQuizThreePoints
                    + numberQuizFourPoints + numberQuizFivePoints + numberQuizSixPoints
                    + numberQuizSevenPoints + numberQuizEightPoints + numberQuizNinePoints
                    + numberQuizTenPoints;


            quizOnePointsText.setText(String.format("%d", numberQuizOnePoints));
            quizTwoPointsText.setText(String.format("%d", numberQuizTwoPoints));
            quizThreePointsText.setText(String.format("%d", numberQuizThreePoints));
            quizFourPointsText.setText(String.format("%d", numberQuizFourPoints));
            quizFivePointsText.setText(String.format("%d", numberQuizFivePoints));
            quizSixPointsText.setText(String.format("%d", numberQuizSixPoints));
            quizSevenPointsText.setText(String.format("%d", numberQuizSevenPoints));
            quizEightPointsText.setText(String.format("%d", numberQuizEightPoints));
            quizNinePointsText.setText(String.format("%d", numberQuizNinePoints));
            quizTenPointsText.setText(String.format("%d", numberQuizTenPoints));
            actualPointsText.setText(String.format("%d", totalPoints));
        }//end if
这是每个测验包的外观,除了为相应测验适当命名的变量和字符串名。。。我开始想,也许这不是从另一个活动中获取传递给主活动的int值的最佳方法,但我想不出其他方法来实现它。。。在这个例子中,我试图保存我马上从测验中得到的numberQuizPoints的值,并从所有其他int变量中得到点数的值,然后在应用程序的主屏幕上显示所有。。。但这不起作用:

下面是一个测试按钮setOnClickListener后面的代码示例:

//Directs user to Quiz One, first screen
        quizOneButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //Subtract out any points that user already got for Quiz 1 from total...
                //In case user wants to or has to re-take Quiz 1
                totalPoints = totalPoints - numberQuizOnePoints;

                //Subtract out any points that user already got for Quiz 1 from itself, equal 0
                numberQuizOnePoints -= numberQuizOnePoints;



                //Go to the Quiz One, first screen

                Intent directToQuizOnePartOne = new Intent();
                directToQuizOnePartOne.setClass(getBaseContext(), QuizOnePartOne.class);
                startActivity(directToQuizOnePartOne);
                finish();
            }//end void onClick quizOne
        });//end setOnClickListener quizOne
这是给定测验的最后一个活动的代码:在本例中,测验1的最后一个活动,将分数传递回Mainacity的活动:

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

public class QuizOnePartFour extends AppCompatActivity
{
    //Create variables for button and TextViews

    TextView totalPointsQuizOneTextView;
    Button returnToMainOne;
    int actualPointsAllQuizOne;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_one_part_four);

        Bundle bundleFour = getIntent().getExtras();
        actualPointsAllQuizOne = bundleFour.getInt("CarryOverThreeQuizOne");

        //Assign variables to appropriate buttons and TextViews

        totalPointsQuizOneTextView = (TextView) findViewById(R.id.txtTotalPointsQuizOne);
        returnToMainOne = (Button) findViewById(R.id.btnReturnToMainOne);

        totalPointsQuizOneTextView.setText(String.format("%d", actualPointsAllQuizOne));

        returnToMainOne.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //Return to main menu screen

                Bundle bundleFive = new Bundle();
                bundleFive.putInt("PointsFromAllQuizOne", actualPointsAllQuizOne);

                Intent directToMainMenuOne = new Intent();
                directToMainMenuOne.setClass(getBaseContext(), MainMenu.class);
                directToMainMenuOne.putExtras(bundleFive);
                startActivity(directToMainMenuOne);
                finish();
            }//end void onClick returnToMainOne
        });//end setOnClickListener returnToMainOne
    }//end void onCreate QuizOnePartFour
}//end class QuizOnePartFour
不要手动调用onStop这些是特殊的方法,您重写它们只是为了执行一些清理


它是活动生命周期方法之一。您应该将该代码移动到按钮单击事件。

使用用户定义函数更新MainActivity中的分数以更新SharedReferenceAccess中的分数:您能提供一个用户定义函数的示例供我使用吗?我对安卓系统的研究还是有点陌生现在的情况是什么