So I have a main class and another class that has a variable I need to pull out into the main class. I have tried some of the ways posted on the answered questions like this, but I’m still failing to get it right.
public class Example extends MapActivity
{
public void OnCreate(Bundle savedInstanceState){
final Button bttn = (Button)findViewById(R.id.button1);
bttn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast tulos = Toast.makeText(getBaseContext(),
"Area is: "
+MyItemizedOverlay.alue +""
/*I get the non static variable error here
which I get as it is not yet defined, it will be after the user
inputs values into the program*/
,Toast.LENGTH_LONG);
tulos.show();
}
});
public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
int alue;
//irreleveant stuff (I assume)
public void Calc(Geopoint gp, Mapview map){
//there's some stuff before the variable I want to get like other variables
//not relevant for my problem I hope
alue = (int)Math.round(*formula: derived from user input data*)
}
}
So how can I get a value out of my other class, as it doesn’t seem to be able to get it now? Or is this maybe an indication of a bigger problem?
There are two way
1 –
make alue staticbut that will show theerror that satic variable can't be accesses in non static function Calcso eithermake function Calcalso static as wellor go to point 22-try this
new MyItemizedOverlay().alue with making alue publicas below