Take the 2-minute tour ×

I am making an android app just to test for myself. I am trying to draw a bitmap image using canvas, but why isn't it working? It just closes itself a second after startup without any exceptions.

This is the code that i am using so far:

package com.mysoftwaremobileapps.BitmapExample;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;

public class BitmapExampleActivity extends Activity {
private ArrayList<CustomImageViewActivity> CustomImages;
Bitmap ImageView1;
float x;
float y;
public void onCreate(Bundle savedInstanceState) {
    Canvas c = new Canvas();
public void doDraw(Canvas canvas)
    for (CustomImageViewActivity p: CustomImages) {
        canvas.drawBitmap(ImageView1, p.getX(), p.getY(), null);

public void drawImage(Canvas canvas)
    ImageView1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    Paint black;
    black = new Paint();
    canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), black);
    canvas.drawBitmap(ImageView1, null, null);

    //Parachuter nr.1
    x = 14;
    y = 28;

    CustomImageViewActivity p = new CustomImageViewActivity(x, y);
    Canvas c1 = new Canvas();

public boolean onTouchEvent(MotionEvent event)
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        for (CustomImageViewActivity p: CustomImages) {
            p.posX = event.getX();
            p.posY = event.getY();
    return false;
share|improve this question
Canvas c = null;doDraw(c); you are passing null –  ρяσѕρєя K Apr 2 '12 at 16:17
All of them works, thanks! –  user1183066 Apr 2 '12 at 19:19
add comment

3 Answers

up vote 0 down vote accepted
Canvas c = null; 

Plz initialize it

Canvas c = new Canvas();
share|improve this answer
add comment

The first problem is that when you call doDraw you're passing in a null reference to a canvas.

To achieve your goal, you could extend a View and override the onDraw method of the View class. That will allow you to draw on the screen.

share|improve this answer
add comment

According to your log, the error is on line 28. Check no further: you initialized your Canvas variable as null, therefore it's obvious that the NullPointerException happens while you try to access one of its methods.

I suggest you another way to draw stuff into an Activity: subclass a View and use the onDraw() method which provides you a Canvas.

share|improve this answer
add comment

Your Answer


