Reputation: 6227
I'm trying to implement two mwthods to save variable informaton after I leave the activity,but I'm getting an error: markOne cannot be resolved to a variable
on each of the variables.I have placed the two methods outside of OnCreate() .I have posted the whole class below showing how I implemented the onSaveInstanceState
and onRestoreInstanceState
methods.Could someone show me why the variales are not being resolved? I'm guessing its because the variables can't be accessed outside of onCreate() but how would I fix this problem?
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", "");
}
}
Upvotes: 1
Views: 1822
Reputation: 228
you have to define global variable below line
TextView result1,result2,result3;
double markOne,markTwo,markThree;
and replace
double markOne = intent.getDoubleExtra("number1", 0);
double markTwo = intent.getDoubleExtra("number2", 0);
double markThree = intent.getDoubleExtra("number3", 0);
with
markOne = intent.getDoubleExtra("number1", 0);
markTwo = intent.getDoubleExtra("number2", 0);
markThree = intent.getDoubleExtra("number3", 0);
Upvotes: 1