I built two activities and the MainActivity is passing some variables to the activity “Calculation”. This works as intended and the variables are submitted and received correctly. I now want to create the integer “size_int” depending on the values of the intent “size”. The problem occurs in this line:
debug1.setText(size_int);
Eclipse tells me that I should create a local variable with the name “size_int”. I do not understand why “size_int” can not be used in this line because it has been defined in the if statement before. Do you have any ideas on that? I assume it has something to do that the variable “size_int” is being defined in the if statement but I am not sure.
Here is the full code:
package com.example.eggtimer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Calculation extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculation);
// get Intents (Size, Temperature and yolk from Main Activity)
String size = getIntent().getExtras().getString("size");
String temperature = getIntent().getExtras().getString("temperature");
String yolk = getIntent().getExtras().getString("yolk");
if (size.equals("Small")) {
int size_int = 30;
}
// Debug Variables
TextView debug1 = (TextView) findViewById(R.id.textViewDebug1);
debug1.setText(size_int);
}
}
Change like below. This is because if you declare inside the braces, the scope is restricted, so you need to increase scope by declaring outside