有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在我的安卓项目中,java在轮换中保存数据似乎不起作用

请注意:我的问题的解决方案是底部的粗体文本。我接受了梅尔奎德斯的回答,因为他帮我过滤掉了所有可能出现问题的东西。事实证明,问题出在我身上,而不是安卓或其他任何东西。因此,如果你正在寻找答案,请阅读下面的内容

我正试图将变量的状态保存为调用onPause();onStop();onDestroy();之前的状态

我正在使用的书让我重写了一个名为

public void onSaveInstanceState(Bundle savedInstanceState){
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putInt(KEY_INDEX, myIntVaraible);
}

您在参数中看到的变量在类的开头声明

private static final String KEY_INDEX = "index";
private int myIntVariable;

创建了这个方法后,这本书告诉我,然后使用onCreate方法并添加

if(savedInstanceState != null){
   myIntVariable = savedIntanceState.getInt(KEY_INDEX, 0);
}

但这是行不通的。 无论何时销毁和创建活动,myIntVariable都会重置为0

我所做的就是去我的清单文件 添加了安卓:configChanges="orientation|screenSize"

然而,我读到这是不切实际的,强烈建议不要这样做

编辑:根据建议,我正在添加我的onCreate();onResume();方法

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate()");
        setContentView(R.layout.activity_main);


        iterateQuestions();


        mTrueButton = (Button)findViewById(R.id.trueBt);
         mTrueButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    correctPressed = true;

                    checkForTrue();
                }
            });


        mFalseButton = (Button)findViewById(R.id.falseBt);
         mFalseButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    falsePressed = false;
                   checkForFalse();
                }
            });

         mNextButton = (ImageButton)findViewById(R.id.nextBt);
          mNextButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v){
             try{
                 mIndexCounter++;
                 mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
                 int QuestionToShow = mQuestionBank[mIndexCounter].getQuestion();
                 mTextViewQuestion.setText(QuestionToShow);
             }
             catch(Exception e){
                 iterateQuestions();
             } 
         }
         });


         mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
         mTextViewQuestion.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v){
                 try{
                     mIndexCounter++;
                     mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
                     int QuestionToShow = mQuestionBank[mIndexCounter].getQuestion();
                     mTextViewQuestion.setText(QuestionToShow);
                 }
                 catch(Exception e){
                     iterateQuestions();
                 } 
             }
         }); 

         mPrevButton = (ImageButton)findViewById(R.id.prevBtn);
         mPrevButton.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View v){ 
                 try{
                     mIndexCounter--;
                     mTextViewQuestion = (TextView)findViewById(R.id.text_question_view);
                     int QuestionToShow = mQuestionBank[mIndexCounter].getQuestion();
                     mTextViewQuestion.setText(QuestionToShow);
                }catch(Exception e){
                    iterateQuestionsReverse();
                }       
            }
         });
    }

@Override
    public void onResume(){
        super.onResume();
        Log.d(TAG,"onResume()");
    }

出于所有目的,变量mIndexCounter就是我提到的“myIntVariable”

解决方案:我正在使用一本书,不幸的是,由于我是安卓编程新手,我太依赖书中编写的代码了。作者通常在他们的书中添加新代码作为粗体黑色文本。这一次,他们没有做到这一点,我很难弄清楚为什么我的数据没有被保存。事实证明它一直都是保存的,我只是无法在检索时使用保存的数据更新视图。在添加了3行简单代码后,我的错误显而易见,我一直试图实现的目标是成功。 我的程序显示了一个文本字符串,该字符串依赖于用于从R.java类检索信息的int。启动应用程序后,当用户按下“下一步”时,数据会发生变化,因为int会增加,视图上的字符串也会发生变化。 由于安卓会在方向更改时销毁任何未保存的数据,因此需要保存此数据。 我所要做的就是简单地添加保存的数据,一个int,与我最初显示这个字符串/文本的方式相同。相反,我愚蠢地认为它会自动执行,因为这本书没有添加此代码,而且我太依赖它了。 这是一次很棒的学习经历,如果有人遇到类似的事情,如果我的答案不清楚,请随时给我发电子邮件


共 (2) 个答案

  1. # 1 楼答案

    在onRestoreInstanceState()中还原变量,而不是在onCreate()中:

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    
        myIntVariable = savedIntanceState.getInt(KEY_INDEX, 0);
    }
    

    docs还说:

    Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

    顺便说一句,将onCreate()签名更改为:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    

    就像在文件里一样

  2. # 2 楼答案

    试试这个:

    android:configChanges="keyboard|keyboardHidden|orientation"
    

    如前所述here