Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在onSavedInstanceState(bundle savedInstanceState)中存储和检索存储的数据时出现问题_Android_Key Value Coding - Fatal编程技术网

Android 在onSavedInstanceState(bundle savedInstanceState)中存储和检索存储的数据时出现问题

Android 在onSavedInstanceState(bundle savedInstanceState)中存储和检索存储的数据时出现问题,android,key-value-coding,Android,Key Value Coding,考虑以下mCreate,etc是动态增加的变量mCreate++。我需要存储和检索它们的最新增量值。我认为我做得对,但有些人认为他们没有应用最新的增量值 @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putInt(CREATE_KEY, mCreate); savedInstanceState.putInt( RESTART_KEY, mR

考虑以下mCreate,etc是动态增加的变量
mCreate++
。我需要存储和检索它们的最新增量值。我认为我做得对,但有些人认为他们没有应用最新的增量值

  @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {

    savedInstanceState.putInt(CREATE_KEY, mCreate);
    savedInstanceState.putInt( RESTART_KEY, mRestart);
    savedInstanceState.putInt(START_KEY, mStart);
    savedInstanceState.putInt(RESUME_KEY, mResume);

    super.onSaveInstanceState(savedInstanceState);

 }

 //////////// get 

  if (savedInstanceState != null) {

       mCreate = savedInstanceState.getInt(CREATE_KEY, mCreate);
       mRestart = savedInstanceState.getInt( RESTART_KEY, mRestart);
       mStart = savedInstanceState.getInt(START_KEY, mStart);
       mResume = savedInstanceState.getInt(RESUME_KEY, mResume);

    }

代码应该是这样的:

 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt(CREATE_KEY, mCreate);
    savedInstanceState.putInt(RESTART_KEY, mRestart);
    savedInstanceState.putInt(START_KEY, mStart);
    savedInstanceState.putInt(RESUME_KEY, mResume);    
 }

 if (savedInstanceState != null) {
    mCreate = savedInstanceState.getInt(CREATE_KEY);
    mRestart = savedInstanceState.getInt(RESTART_KEY);
    mStart = savedInstanceState.getInt(START_KEY);
    mResume = savedInstanceState.getInt(RESUME_KEY);
 }
你想要这个

private TextView mTvCreate;
private TextView mTvStart;
private TextView mTvResume;
private TextView mTvRestart;

private int mCreate = 0;
private int mStart = 0;
private int mResume = 0;
private int mRestart = 0;
// TODO: Create variables for each of the TextViews, called
    // mTvCreate, etc. 

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

    // TODO: Assign the appropriate TextViews to the TextView variables
    // Hint: Access the TextView by calling Activity's findViewById()
    // textView1 = (TextView) findViewById(R.id.textView1);
    mTvCreate = (TextView) findViewById(R.id.create);
    mTvStart = (TextView) findViewById(R.id.start);
    mTvResume = (TextView) findViewById(R.id.resume);
    mTvRestart = (TextView) findViewById(R.id.restart);




    Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); 
    launchActivityTwoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO:
            // Launch Activity Two
            // Hint: use Context's startActivity() method

            // Create an intent stating which Activity you would like to start


            // Launch the Activity using the intent
            Intent startActivity = new Intent(ActivityOne.this, ActivityTwo.class);
            startActivity(startActivity);


        }
    });

    // Check for previously saved state
    if (savedInstanceState != null) {

        // TODO:
        // Restore value of counters from saved state
        // Only need 4 lines of code, one for every count variable
        mCreate = savedInstanceState.getInt(CREATE_KEY);
        mStart = savedInstanceState.getInt(START_KEY);
        mResume = savedInstanceState.getInt(RESUME_KEY);
        mRestart = savedInstanceState.getInt(RESTART_KEY);

    }

    ++mCreate;
    // TODO: Emit LogCat message
    Log.i(TAG, "onCreate() "+mCreate);
    displayCounts();

    // TODO:
    // Update the appropriate count variable
    // Update the user interface via the displayCounts() method



}

// Lifecycle callback overrides

@Override
public void onStart() {
    super.onStart();

    // TODO: Emit LogCat message
    ++mStart;
    Log.i(TAG, "onStart() "+mStart);
    // TODO:
    // Update the appropriate count variable
    // Update the user interface
    displayCounts();

}

@Override
public void onResume() {
    super.onResume();

    // TODO: Emit LogCat message

    ++mResume;
    Log.i(TAG, "onResume() "+mResume);
    // TODO:
    // Update the appropriate count variable
    // Update the user interface
    displayCounts();

}

@Override
public void onPause() {
    super.onPause();

    // TODO: Emit LogCat message

}

@Override
public void onStop() {
    super.onStop();

    // TODO: Emit LogCat message

}

@Override
public void onRestart() {
    super.onRestart();

    // TODO: Emit LogCat message

    ++mRestart;

    // TODO:
    // Update the appropriate count variable
    // Update the user interface
    Log.i(TAG, "onRestart() "+mRestart);
    displayCounts();

}

@Override
public void onDestroy() {
    super.onDestroy();

    // TODO: Emit LogCat message


}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
                 super.onSaveInstanceState(savedInstanceState);
    // TODO:
    // Save state information with a collection of key-value pairs
    // 4 lines of code, one for every count variable
    savedInstanceState.putInt(CREATE_KEY, mCreate);
    savedInstanceState.putInt(START_KEY, mStart);
    savedInstanceState.putInt(RESUME_KEY, mResume);
    savedInstanceState.putInt(RESTART_KEY, mRestart);






}

// Updates the displayed counters
public void displayCounts() {

    mTvCreate.setText("onCreate() calls: " + mCreate);
    mTvStart.setText("onStart() calls: " + mStart);
    mTvResume.setText("onResume() calls: " + mResume);
    mTvRestart.setText("onRestart() calls: " + mRestart);

}

我怎么会错过这么一个小问题,谢谢。顺便说一句,我也在上这门课。随便问一些关于实验室的问题:)@localhost:在哪个讲座中讨论了“拯救国家”这个话题?我想不是。在第二个实验中,你们应该在场外阅读文档,然后自己实现。伙计们,我有另一个问题,我现在就发布它。我发现你们的代码和OP@jangle现在再试一次