In my application, I have many classes and activities. A droid is a class that has no context. Mygame is a class that extends SurfaceView and implements SurfaceHolder.Callback. I create a Droid object in the mygame class and set the background image and position for it. The code I wrote for this is below.
block1 = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.birdpic), 100, 10);
The constructor of the Droid class is given below.
public Droid(Bitmap bitmap, int x, int y) { this.bitmap = bitmap; this.x = x; this.y = y; }
In a specific scenario, I have to set the background image and the position of the Droid object from the Droid class itself. So I ran into a problem. Below is a code snippet for this.
if(checkflag) { myObj.centerblock=new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.blast), myObj.xpos, myObj.ypos); }
The problem is that the Droid class has no context. Therefore, I cannot use getResources () here. I tried the code below, but it crashes.
if(checkflag) { myObj.centerblock=new Droid(BitmapFactory.decodeResource(myObj.getResources(), R.drawable.blast), myObj.xpos, myObj.ypos); }
Can someone help me. I just want to set a background image and place it for a Droid object from the Droid class itself.
android android-context class background
andro-girl Nov 23 '11 at 7:30 2011-11-23 07:30
source share