Let me start off with I’m mostly new to writing Java, so I would love for a complete explanation. Not just a spew of code, but something that gives some in site to why and where it should be
I’m currently attempting to write an Application, how ever I’m have some trouble with the Canvas and drawing the bitmap I would like onto it. This is my code to draw the image:
Canvas canvas = null;
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(image, 10, 800, null);
This method (expectantly) throws a Null Pointer Exception. However, Defining “canvas” as null is the only option Eclipse gives me. When attempting to define it as
Canvas canvas = new Canvas();
Android simply refuses to draw the bitmap. I’m sure my image is located at res/drawable/image
If it helps, the image is saved as “image.png”
How would I make Android display/display my image/bitmap at the specific location I have asked it to draw the image (10×800)?
Writing nonsense that solves a compiler error doesn’t change the fact that it’s nonsense! 🙂
You have to get a reference to a Canvas object another way. You might want to be more specific about what you are exactly trying to do, so that we can suggest how you should go about it. (Ex. are you attempting to just display an image along with some other Views? Are you trying to create a custom View? You might just want to consider using an ImageView)
Edit:
You should read up about Android architecture at developer.android.com . If you are simply trying to show an image, there might not be a reason to use the canvas directly. Nonetheless, you can draw within a custom View by extending the View class
if you don’t need a custom view, just use the ImageView class
Warning: I wrote those method calls off of the top of my head, they might be slightly off.