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

2つの方法があります

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 に答える