[Java for Android App Development] Wampirate:
Hey, I have a new question :) It is so strange. You know how my whole game has a thread in the back doing the drawing and updating in game state? I want to create the thread when changing to game state only, and making the menu non-thread, because my phone overheats on menu, when that thread loops in the background.

The thing is that there is something about onSizeChanged() method, I cannot draw from there? You'll see what I mean here:

I made another project just to test that

package com.example.menutest;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

private myView mv;
[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
mv = new myView(this);
setVisible(true);
setContentView(mv);
}

public class myView extends SurfaceView{
private SurfaceHolder holder;
private Bitmap background;
private Canvas c;
private Paint myPaint;
private float x=100,y=100;
public myView(Context context) {
super(context);
holder = getHolder();
myPaint = new Paint();
// TODO Auto-generated constructor stub
}

private void redraw(){
c= null;
c = holder.lockCanvas();
synchronized(holder){
doDrawing(c);
}
holder.unlockCanvasAndPost(c);
}

private void doDrawing(Canvas d){
myPaint.setColor(Color.RED);
d.drawBitmap(background,0,0,null);
//d.drawARGB(255, 0, 0, 0);
d.drawCircle(x, y, 20, myPaint);
}

[MENTION=439709]override[/MENTION]
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
break;
}
redraw();
return true;
}

[MENTION=439709]override[/MENTION]
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
background = BitmapFactory.decodeResource(getResources(), R.drawable.bg,options);
background = Bitmap.createScaledBitmap(background, w, w, true);
//redraw();
//Canvas c = null;
//draw(c);
//onDraw(c);
}

[MENTION=439709]override[/MENTION]
public void draw(Canvas c) {
super.draw(c);
myPaint.setColor(Color.RED);
//d.drawBitmap(background,0,0,null);
c.drawARGB(255, 0, 0, 0);
c.drawCircle(x, y, 20, myPaint);
}

[MENTION=439709]override[/MENTION]
protected void onDraw(Canvas c) {
super.onDraw(c);
myPaint.setColor(Color.RED);
//d.drawBitmap(background,0,0,null);
c.drawARGB(255, 0, 0, 0);
c.drawCircle(x, y, 20, myPaint);
}

}

}

What we can see here is a simple app basicly when you touch it redraws and the circle will be in that position. That's how my menu I want to work - you touch and it redraws. But the problem is with the first drawing.

When I start the app it shows black screen, when I touch it draws the background bitmap and the circle. How do I make it draw once initially? You can see I commented redraw(), draw(c), and onDraw(c), I tried all that functions and each one of them gives me a null pointer exception for anything like

canvas.draw... even ARGB or circles or rectangles. Why is that, I mean what am I missing here?

EDIT: Found the solution in the net!

Had to add this to onSizeChanged()

Handler handler = new Handler();
handler.post(new Runnable() {

[MENTION=439709]override[/MENTION]
public void run() {
redraw();
}
});

Not really getting it but it works :)