Main class has two variables that want to access another class:
public class MyClassA extends Activity {
int i = 1;
Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.i = 31;
this.b1 = (Button) findViewById(R.id.btn1);
~~
}
}
Second class want to call variables in mainClass object:
public class MyclassB implements OnClickListener{
MyClassA mainClass = new MyClassA();
Button btn = mainClass.b1;
int n = mainClass.i;
public void OnClick(View arg0){
Log.v("btn:",btn);
Log.v("int:",n);
}
//btn returns null;
//int returns 1;
But onCreate method not set variables..
Why not set main class variables like this.i=31 ?
When you just instantiate your activity like a simple class, it won’t execute
onCreate()that is whyivalue stays as1. onCreate() will be called when you useIntentandActivity.As commented, you may need to use either Inner classes (or) anonymous class. read this documentation for more info.