我正在尝试在离开活动后实现两个方法以保存变量信息,但我在每个变量上都收到错误:markOne cannot be resolved to a variable
。我已将两个方法放在OnCreate之外(我已经发布了下面的全班,展示了我如何实现onSaveInstanceState
和onRestoreInstanceState
方法。有人可以告诉我为什么变量没有被解决?我猜它是因为变量不能在onCreate()之外访问,但我该如何解决这个问题呢?
public class CalcResult extends Activity implements OnClickListener{
TextView result1,result2,result3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
final Intent intent1=new Intent(this,AboutActivity.class);
final Intent intent2=new Intent(this,MainActivity.class);
final Intent intent3=new Intent(this,MainActivity.class);
final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(
R.layout.a,
null);
// Set up your ActionBar
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
// You customization
final int actionBarColor = getResources().getColor(R.color.action_bar);
actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));
final Button actionBarHome = (Button) findViewById(R.id.action_bar_title);
actionBarHome.setBackgroundResource(R.drawable.ic_action_back);
actionBarHome.setOnClickListener(this);
actionBarHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent2);
}
});
final Button actionBarInfo = (Button) findViewById(R.id.action_bar_staff);
actionBarInfo.setBackgroundResource(R.drawable.ic_action_help);
actionBarInfo.setOnClickListener(this);
actionBarInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent1);
}
});
final Button actionBarHoome = (Button) findViewById(R.id.action_bar_home);
actionBarHoome.setBackgroundResource(R.drawable.appicon);
actionBarHoome.setOnClickListener(this);
actionBarHoome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(intent3);
}
});
result1 = (TextView)findViewById(R.id.markOne);
result2 = (TextView)findViewById(R.id.markTwo);
result3 = (TextView)findViewById(R.id.markThree);
Intent intent = getIntent();
double markOne = intent.getDoubleExtra("number1", 0);
double markTwo = intent.getDoubleExtra("number2", 0);
double markThree = intent.getDoubleExtra("number3", 0);
DecimalFormat df = new DecimalFormat("#.##");
result1.setText(String.valueOf(df.format(markOne)+"mm"));
result2.setText(String.valueOf(df.format(markTwo)+"mm"));
result3.setText(String.valueOf(df.format(markThree)+"mm"));
}
//Two methods I'm trying to implement ->
@Override
protected final void onSaveInstanceState(final Bundle outState)
{
// Save variables.
outState.putString("markOne", markOne);
outState.putString("markTwo", markTwo);
outState.putString("markThree", markThree);
}
@Override
protected final void onRestoreInstanceState(final Bundle outState)
{
// Restore saved variables and reshow activities if they were open.
markOne = outState.getString("markOne", "");
markTwo = outState.getString("markTwo", "");
markThree = outState.getString("markThree", "");
}
}
答案 0 :(得分:1)
你必须在行
下面定义全局变量TextView result1,result2,result3;
double markOne,markTwo,markThree;
并替换
double markOne = intent.getDoubleExtra("number1", 0);
double markTwo = intent.getDoubleExtra("number2", 0);
double markThree = intent.getDoubleExtra("number3", 0);
与
markOne = intent.getDoubleExtra("number1", 0);
markTwo = intent.getDoubleExtra("number2", 0);
markThree = intent.getDoubleExtra("number3", 0);