I try to understand how display a bitmap on a surface. For it, I use the code from LunarLander and the GameTemplate of this forum
I have a problem when I start my application on emulator, I obtain an error message.
"The aplication has stopped unexpectedly. Please try again"
I have the same problem with the GameTemplate if I change the package path. I really don't understand the error, even when I use the debug mode step by step. For that I put my source code here :
GraphicAppTest.java
Using java Syntax Highlighting
- package com.example.graphicapptest;
- import com.example.graphicapptest.GraphicAppTestView.GraphicAppThread;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Window;
- public class GraphicAppTest extends Activity {
- public GraphicAppTestView mGraphicAppTestView;
- public GraphicAppThread mGraphicAppThread;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- mGraphicAppTestView = (GraphicAppTestView) findViewById(R.id.graphictest);
- mGraphicAppThread = mGraphicAppTestView.getThread();
- mGraphicAppThread.doStart();
- }
- protected void onPause() {
- super.onPause();
- mGraphicAppTestView.getThread().pause(); // pause game when Activity pauses
- }
- }
Parsed in 0.014 seconds, using GeSHi 1.0.8.4
GraphicAppTestView.java
Using java Syntax Highlighting
- package com.example.graphicapptest;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.os.Handler;
- import android.os.Message;
- import android.util.AttributeSet;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class GraphicAppTestView extends SurfaceView implements SurfaceHolder.Callback {
- public class GraphicAppThread extends Thread {
- /** Message handler used by thread to post stuff back to the GameView */
- private Handler mHandler;
- /** Handle to the surface manager object we interact with */
- private SurfaceHolder mSurfaceHolder;
- private boolean mRun = false;
- private Bitmap mBitmapTest;
- private int mCanvasWidth;
- private int mCanvasHeight;
- public GraphicAppThread(SurfaceHolder surfaceHolder, Context context,
- Handler handler) {
- // TODO Auto-generated constructor stub
- mSurfaceHolder = surfaceHolder;
- mHandler = handler;
- mContext = context;
- mBitmapTest = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sheepicon);
- }
- public void doStart(){
- synchronized (mSurfaceHolder) {
- // Init
- mRun = true;
- }
- }
- public void run() {
- while (mRun) {
- Canvas c = null;
- try {
- c = mSurfaceHolder.lockCanvas(null);
- synchronized (mSurfaceHolder) {
- 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){
- canvas.drawARGB(255, 0, 0, 0);
- canvas.drawBitmap(mBitmapTest, 10, 10, new Paint());
- }
- public void pause(){
- }
- /* Callback invoked when the surface dimensions change. */
- public void setSurfaceSize(int width, int height) {
- // synchronized to make sure these all change atomically
- synchronized (mSurfaceHolder) {
- mCanvasWidth = width;
- mCanvasHeight = height;
- }
- }
- }
- private GraphicAppThread thread;
- private Context mContext;
- public GraphicAppTestView(Context context, AttributeSet attrs) {
- super(context, attrs);
- SurfaceHolder holder = getHolder();
- holder.addCallback(this);
- thread = new GraphicAppThread(holder, context, new Handler() {
- @Override
- public void handleMessage(Message m) {
- // Use for pushing back messages.
- }
- });
- setFocusable(true); // make sure we get key events
- }
- public GraphicAppThread getThread(){
- return thread;
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- thread.setSurfaceSize(width, height);
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- thread.start();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- }
- }
Parsed in 0.016 seconds, using GeSHi 1.0.8.4
main.xml
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout android:id="@+id/FrameLayout01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <com.example.graphicapptest.GraphicAppTestView
- android:id="@+id/graphictest"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"/>
- <LinearLayout android:id="@+id/LinearLayout01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:text="@+id/TextView01"
- android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
- </FrameLayout>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
I add the logcat
LOGCAT
09-25 18:46:16.183: INFO/jdwp(766): received file descriptor 20 from ADB
09-25 18:46:16.541: WARN/Resources(766): Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f050003}
09-25 18:46:16.551: DEBUG/AndroidRuntime(766): Shutting down VM
09-25 18:46:16.551: WARN/dalvikvm(766): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
09-25 18:46:16.561: ERROR/AndroidRuntime(766): Uncaught handler: thread main exiting due to uncaught exception
09-25 18:46:16.571: ERROR/AndroidRuntime(766): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.graphicapptest/com.example.graphicapptest.GraphicAppTest}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.os.Looper.loop(Looper.java:123)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.ActivityThread.main(ActivityThread.java:3948)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at java.lang.reflect.Method.invoke(Method.java:521)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at dalvik.system.NativeStart.main(Native Method)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:281)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.Activity.requestWindowFeature(Activity.java:2556)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at com.example.graphicapptest.GraphicAppTest.onCreate(GraphicAppTest.java:20)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
09-25 18:46:16.571: ERROR/AndroidRuntime(766): ... 11 more
09-25 18:46:16.601: INFO/Process(567): Sending signal. PID: 766 SIG: 3
09-25 18:46:16.601: INFO/dalvikvm(766): threadid=7: reacting to signal 3
09-25 18:46:16.651: INFO/dalvikvm(766): Wrote stack trace to '/data/anr/traces.txt'
09-25 18:46:25.904: WARN/ActivityManager(567): Launch timeout has expired, giving up wake lock!