它是一个小程序我试图在android中学习状态,但我不知道当我点击我的按钮计数增量但旋转屏幕值没有得到商店我不知道什么是错的。它Succesffuly显示它进入的吐司,但显示计数为零。
package com.example.na462.layoutsample2;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Na462 on 8/7/2017.
*/
public class States extends AppCompatActivity {
Button button;
TextView textView ;
int Count = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.states);
textView = (TextView)findViewById(R.id.textView);
textView.setText("You Pressed: " + Count + "Times");
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Count++;
textView.setText("You Pressed: " + Count + "Times");
}
});
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putInt("Counter",Count);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Toast.makeText(this, "Entered", Toast.LENGTH_SHORT).show();
Count = savedInstanceState.getInt("Counter");
Toast.makeText(this, "Count is: " + Count, Toast.LENGTH_SHORT).show();
textView.setText("You Pressed: " + Count + "Times");
}
}
答案 0 :(得分:0)
您应使用此版本的PersistableBundle
(不含onRestoreInstanceState
参数),因为您使用的@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
版本没有该参数:
std::remove