I'm having some huge troubles with accessing my Panels and SurfaceHolders and Canvases in my program. The code is below (three separate classes), and the program will crash before it even displays anything. Any tips someone can give would be awesome. This is pretty much copy-and-paste from Google's Lunar Lander, but I apparently messed something up.
and
and
Code:
public class myActivity extends Activity
{
LinearLayout mLinearLayout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
Panel panel = new Panel(getApplicationContext());
}
}
and
Code:
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
private CanvasThread canvasThread;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
canvasThread = new CanvasThread(getHolder(),getContext(),getHandler());
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
canvasThread.setRunning(true);
canvasThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
canvasThread.setRunning(false);
while (retry) {
try {
canvasThread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(icon, 10, 10, null);
}
}
and
Code:
package com.mst.Splat;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.view.SurfaceHolder;
public class CanvasThread extends Thread {
private SurfaceHolder mSurfaceHolder;
private Context mContext;
private Handler mHandler;
private boolean mRun;
public CanvasThread(SurfaceHolder surfaceHolder, Context context,
Handler handler)
{
mSurfaceHolder = surfaceHolder;
mHandler = handler;
mContext = context;
}
public void setRunning(boolean b)
{
mRun = b;
}
public void run()
{
while (mRun) {
Canvas c = null;
try {
c = mSurfaceHolder.lockCanvas(null);
synchronized (mSurfaceHolder) {
//if (mMode == STATE_RUNNING) updatePhysics();
//Later, I'll use the original code -- this is just temp
//this should only be implemented when the game is running
// if(true) updatePhysics();
doDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
public void doDraw(Canvas canvas)
{
// Draw the background image. Operations on the Canvas accumulate
// so this is like clearing the screen.
Bitmap mBackgroundImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.background);
canvas.drawBitmap(mBackgroundImage, 0, 0, null);
Bitmap mIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon);
canvas.drawBitmap(mIcon, 0, 0, null);
}
}