0

所以我有一个主类和另一个有一个变量的类,我需要把它拉到主类中。我已经尝试了一些在这样的已回答问题上发布的方法,但我仍然无法正确处理。

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*)
    }
}

那么我怎样才能从我的其他班级中获得价值,因为它现在似乎无法获得呢?或者这可能是一个更大问题的迹象?

4

2 回答 2

0

有两种方式

1 -make alue static但这将显示error that satic variable can't be accesses in non static function Calc或者make function Calcalso static as well转到第 2 点

2-尝试new MyItemizedOverlay().alue with making alue public如下

  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: "
                  +new 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 has input 
    values into the program*/
                  ,Toast.LENGTH_LONG);
           tulos.show();
             }
         });

    public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
    public 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*)
    }
于 2012-06-27T09:49:28.970 回答
0

没有访问修饰符int alue意味着它是默认的。使其public int alue可以从其他类访问。查看控制对类成员的访问

于 2012-06-27T09:55:20.250 回答