I’m trying to pass context from one class to another.. pls tell me wat am i doing wrong..
Draw.java
public class Draw extends View{
private Context context;
private Paint paint = null;
private Point start,end;
public Draw(Context check){
super(check);
paint();
}
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.drawRect(60,60,120,120,paint);
Check check = new Check(context.getApplicationContext());
check.update();
}
public void paint(){
paint= new Paint();
paint.setColor(Color.YELLOW);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
}
}
Check.java
public class Check {
private static Check check = null;
private Context context;
public Check(Context context){
this.context = context;
}
void update() {
Toast.makeText(context, "Context Received", Toast.LENGTH_LONG).show();
}
}
RectangleActivity.java
public class RectangleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
Context context;
super.onCreate(savedInstanceState);
Draw draw = new Draw(this);
setContentView(draw);
}
}
please help me.. I’m stuck with this problem for days…
in your Draw-constructor, you never save the context to your variable, it should be like in the Check-constructor:
otherwise you will have a
nullwhen you get toWhich is also a bit unnecessary, it is enough to pass the
contextas it is.