Writing nonsense that solves a compiler error does not alter the fact that this is nonsense! :)
You should get a reference to the Canvas object in another way. You can be more specific about what exactly you are trying to do so that we can suggest how you should do it. (For example, are you trying to just display the image along with some other views? Are you trying to create a custom view? You might just want to consider using ImageView)
Edit:
You should read about Android architecture at developer.android.com. If you're just trying to display an image, there may be no reason to use the canvas directly. However, you can draw in a custom view by extending the view class
class myView extends View{ Bitmap bm; loadBitmap() { bm = BitmapFactory.decodeResource(getResources(), R.drawable.image); } @Override public void draw(Canvas c) { c.drawBitmap(bm, XCORD, YCORD, null); } }
if you donβt need a custom view, just use the ImageView class
class MyActivty extends Activity{ @Override public void onCreate(Bundle b) { super.onCreate(b); ImageView iv = new ImageView(this); iv.setImageResource(R.drawable.pic); setContentView(iv); } }
Warning: I wrote these method calls from the top of my head; they may be slightly disabled.
source share