My class A creates in its constructor an instance of class B. Class B's constructor creates an instance of its inner class C. Class C needs a field in its constructor from class A and here is the problem. Class C reaches only the default value of the field of A.
I initialize the value of the field of A in A's constructor before starting the initiation of B and C. I checked that the field has the value I need. Nevertheless, the value of that field in the constructor of C is always the default one. I am not sure how to deal with this. Any help?
Thanks!
Thank you guys for all answers! I felt though that more information is necessary.
This is part of my class A: The problematic variables are startX and startY. I defined them in the constructor either directly, ether through setter method. Class B is DrawView. Class C is DrawThread. I do not know how to pass arguments to the constructor of DrawView, otherwise I would. Help? Another option is to use /*display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
in DrawView or DrawThread to set my startX and startY, but it just does not want to work. This really switches my question more to android now, and I will be grateful for any suggestions how to get screen size in the constructor of DrawThread, which was the ultimate goal of all that:)?
public class ColorAnimation extends Activity {
public static float startX;
public static float startY;
//other stuff here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// turn off the window's title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// tell system to use the layout defined in our XML file
setContentView(R.layout.main);
/*display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
startX = display.getWidth();
startY = display.getHeight();*/
mDrawView = (DrawView) findViewById(R.id.pend);
mDrawThread = mDrawView.getThread();
// give the DrawView a handle to the TextView used for messages
mDrawView.setTextView((TextView) findViewById(R.id.text));
Constructor of class B:
//
public DrawView(Context context, AttributeSet attrs) { super(context, attrs);
// register our interest in hearing about changes to our surface
SurfaceHolder holder = getHolder();
holder.addCallback(this);
thread = new DrawThread(holder, context, new Handler() {
@Override
public void handleMessage(Message m) {
mStatusText.setVisibility(m.getData().getInt("viz"));
mStatusText.setText(m.getData().getString("text"));
}
});
setFocusable(true); // make sure we get key events
}
//part of class C DrawThread:
class DrawView extends SurfaceView implements SurfaceHolder.Callback {
class DrawThread extends Thread {//................
//..............
public DrawThread(SurfaceHolder surfaceHolder, Context context,
Handler handler) {
// get handles to some important objects
mSurfaceHolder = surfaceHolder;
mHandler = handler;
mContext = context;
You could create a function in class A that accesses the value of field A. Then just pass the instance of class A all the way down to class C. Without more details, I don't think I can give a more detailed answer.