Question

I am new to android development. I was trying to make game. In that game I have to show grid of dots (5*6) on canvas. For this purpose I decided to first show circles of same range and then show bitmaps in them. I followed a tutorial for drawing circles. I compiled that code. There is no error or warning there, but canvas don't show any circle. Please suggest me what should I do. Below given is code and logcat out put.

My Code File:

package com.example.circle;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public class board extends View
    {
        Paint p = new Paint();
        Paint black=new Paint();
        Paint blue=new Paint();
        int rows=5;
        int cols=6;
        Bitmap [][] dot=new Bitmap[rows][cols];
        Canvas g=new Canvas();
        public board(Context context) {
            super(context);
            p.setARGB(255, 255, 255, 255);
            black.setARGB(255, 0, 0, 0);
            blue.setARGB(255, 255, 0, 0);
            for(int y=0; y<cols; y++)
            {
                for(int x=0; x<rows; x++)
                {
                    Bitmap map= Bitmap.createBitmap(100,100, Config.RGB_565);
                    dot[x][y]=map;
                    g.setBitmap(dot[x][y]);
                    g.drawCircle(50, 50, 50, black);

                }
            }
        }
        protected void onDraw(Canvas canvas)
        {
            super.onDraw(canvas);
            canvas.drawPaint(p);
            for(int y=0; y<cols; y++)
            {
                for(int x=0; x<rows; x++)
                {
                    canvas.drawBitmap(dot[x][y], x*100, y*100,null);
                }
            }

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

Logcat File:

03-24 12:33:37.162: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x71a620
03-24 12:33:37.165: V/MediaProvider/DrmHelper(28263): synchronized permitAccessDrm(28936) return false
03-24 12:33:37.175: D/IPCThreadState(28263): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x87ac38
03-24 12:33:37.175: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x9bcd28
03-24 12:33:37.176: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x71a620
03-24 12:33:37.180: V/MediaProvider/DrmHelper(28263): synchronized permitAccessDrm(28936) return false
03-24 12:33:37.184: D/IPCThreadState(28263): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x87db38
03-24 12:33:37.184: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x60a430
03-24 12:33:37.186: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x71a620
03-24 12:33:37.251: I/SurfaceFlinger(101): [SurfaceFlinger] frames:2, duration:1.502000, fps:1.331077
03-24 12:33:37.340: D/IPCThreadState(28222): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x75a150
03-24 12:33:37.341: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0xa0c9b0
03-24 12:33:37.342: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x7b20c8
03-24 12:33:37.346: I/CallLogProvider(24184): match == 1
03-24 12:33:37.346: D/CallLogProvider(24184):    In call log providers,  selectionBuilder=((type != '4'))
03-24 12:33:37.364: D/CallLogProvider(24184): query count == 413
03-24 12:33:37.365: D/IPCThreadState(24184): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x971810
03-24 12:33:37.366: D/IPCThreadState(233): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0xd21190
03-24 12:33:37.367: D/IPCThreadState(28936): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x7c7198
03-24 12:33:37.371: V/ContactsProvider(24184): query begin uri [content://com.android.contacts/contacts] selection [  ]
Was it helpful?

Solution

setContentView(R.layout.activity_main);

change this to

setContentView(new board(this));

Since you are not creating a new instance of the class board and setting as the content, this view wont get added. It is conventional to start a class name with capital letter.

Add this inside the onDraw for loop instead of drawing the bitmap.

canvas.drawCircle(50, (x + 1) * 2 * 50, (y + 1) * 2 * 50, black);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top