Domanda

here, how to print ss value from Activity's oncreate() method?

    MainActivity extends Activity {

      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        view1 = (TextView)findViewById(R.id.ss_View);

        //view.setText(String.valueOf(ss)); 
      }

      private class x {
        @Override
        public void func() {
         int ss = 3;
        }
      }
    }
È stato utile?

Soluzione

Make ss an instance variable of the x class and add a getter method:

public class InnerActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        TextView view = (TextView) findViewById(R.id.clickableButtonWithBorder);

        x xInstance = new x();
        xInstance.func();
        view.setText(String.valueOf(xInstance.getSS()));

    }

    private class x {
        int ss;

        public void func() {
            ss = 3;
        }

        public Integer getSS()
        {
            return ss;
        }
    }

}

Altri suggerimenti

You can't do this. SS variable is only created locally in your method. Create ss variable as field of class and generate getter. Then you can get your variable.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top