[SOLVED]Graphic problems

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

[SOLVED]Graphic problems

Postby 3m3r1c » Fri Sep 25, 2009 6:39 pm

Hi,
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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.graphicapptest;
  2.  
  3. import com.example.graphicapptest.GraphicAppTestView.GraphicAppThread;
  4.  
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.view.Window;
  8.  
  9. public class GraphicAppTest extends Activity {
  10.        
  11.         public GraphicAppTestView mGraphicAppTestView;
  12.         public GraphicAppThread mGraphicAppThread;
  13.        
  14.         /** Called when the activity is first created. */
  15.         @Override
  16.         public void onCreate(Bundle savedInstanceState) {
  17.                 super.onCreate(savedInstanceState);
  18.                 setContentView(R.layout.main);
  19.                
  20.                 requestWindowFeature(Window.FEATURE_NO_TITLE);
  21.                
  22.                 mGraphicAppTestView = (GraphicAppTestView) findViewById(R.id.graphictest);
  23.                 mGraphicAppThread = mGraphicAppTestView.getThread();
  24.                
  25.                 mGraphicAppThread.doStart();
  26.         }
  27.        
  28.         protected void onPause() {
  29.                 super.onPause();
  30.                 mGraphicAppTestView.getThread().pause(); // pause game when Activity pauses
  31.         }
  32. }
  33.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



GraphicAppTestView.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.graphicapptest;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.os.Handler;
  9. import android.os.Message;
  10. import android.util.AttributeSet;
  11. import android.view.SurfaceHolder;
  12. import android.view.SurfaceView;
  13.  
  14. public class GraphicAppTestView extends SurfaceView implements SurfaceHolder.Callback {
  15.         public class GraphicAppThread extends Thread {
  16.                
  17.                 /** Message handler used by thread to post stuff back to the GameView */
  18.                 private Handler mHandler;
  19.                 /** Handle to the surface manager object we interact with */
  20.                 private SurfaceHolder mSurfaceHolder;
  21.                
  22.                 private boolean mRun = false;
  23.                
  24.                 private Bitmap mBitmapTest;
  25.                
  26.         private int mCanvasWidth;
  27.         private int mCanvasHeight;
  28.                
  29.                
  30.                 public GraphicAppThread(SurfaceHolder surfaceHolder, Context context,
  31.                 Handler handler) {
  32.                         // TODO Auto-generated constructor stub
  33.                        
  34.                         mSurfaceHolder = surfaceHolder;
  35.                         mHandler = handler;
  36.                         mContext = context;
  37.                        
  38.                         mBitmapTest = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sheepicon);
  39.                 }
  40.                
  41.                
  42.                 public void doStart(){
  43.                         synchronized (mSurfaceHolder) {
  44.                 // Init
  45.                                 mRun = true;
  46.                                
  47.             }
  48.                 }
  49.                
  50.                 public void run() {
  51.                         while (mRun) {
  52.                 Canvas c = null;
  53.                 try {
  54.                     c = mSurfaceHolder.lockCanvas(null);
  55.                     synchronized (mSurfaceHolder) {
  56.                         doDraw(c);
  57.                     }
  58.                 } finally {
  59.                     // do this in a finally so that if an exception is thrown
  60.                     // during the above, we don't leave the Surface in an
  61.                     // inconsistent state
  62.                     if (c != null) {
  63.                         mSurfaceHolder.unlockCanvasAndPost(c);
  64.                     }
  65.                 }
  66.             }
  67.                 }
  68.                
  69.                 public void doDraw(Canvas canvas){
  70.                         canvas.drawARGB(255, 0, 0, 0);
  71.                 canvas.drawBitmap(mBitmapTest, 10, 10, new Paint());
  72.                 }
  73.                
  74.                 public void pause(){
  75.                        
  76.                 }
  77.                
  78.                 /* Callback invoked when the surface dimensions change. */
  79.         public void setSurfaceSize(int width, int height) {
  80.             // synchronized to make sure these all change atomically
  81.             synchronized (mSurfaceHolder) {
  82.                 mCanvasWidth = width;
  83.                 mCanvasHeight = height;
  84.             }
  85.         }
  86.         }
  87.        
  88.         private GraphicAppThread thread;
  89.         private Context mContext;
  90.        
  91.         public GraphicAppTestView(Context context, AttributeSet attrs) {
  92.                 super(context, attrs);
  93.                
  94.                 SurfaceHolder holder = getHolder();
  95.         holder.addCallback(this);
  96.  
  97.                
  98.                 thread = new GraphicAppThread(holder, context, new Handler() {
  99.             @Override
  100.             public void handleMessage(Message m) {
  101.                 // Use for pushing back messages.
  102.             }
  103.         });
  104.                
  105.                 setFocusable(true); // make sure we get key events
  106.         }
  107.  
  108.         public GraphicAppThread getThread(){
  109.                 return thread;
  110.         }
  111.        
  112.        
  113.        
  114.         @Override
  115.         public void surfaceChanged(SurfaceHolder holder, int format, int width,
  116.                         int height) {
  117.                 // TODO Auto-generated method stub
  118.                 thread.setSurfaceSize(width, height);
  119.  
  120.         }
  121.  
  122.         @Override
  123.         public void surfaceCreated(SurfaceHolder holder) {
  124.                 // TODO Auto-generated method stub
  125.                 thread.start();
  126.  
  127.         }
  128.  
  129.         @Override
  130.         public void surfaceDestroyed(SurfaceHolder holder) {
  131.                 // TODO Auto-generated method stub
  132.  
  133.         }
  134.  
  135. }
  136.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <FrameLayout android:id="@+id/FrameLayout01"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. xmlns:android="http://schemas.android.com/apk/res/android">
  7.  
  8.         <com.example.graphicapptest.GraphicAppTestView
  9.         android:id="@+id/graphictest"
  10.    android:layout_width="fill_parent"
  11.    android:layout_height="fill_parent"/>
  12.      
  13.         <LinearLayout android:id="@+id/LinearLayout01"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content">
  16.                 <TextView android:text="@+id/TextView01"
  17.                 android:id="@+id/TextView01"
  18.                 android:layout_width="wrap_content"
  19.                 android:layout_height="wrap_content"></TextView>
  20.         </LinearLayout>
  21. </FrameLayout>
  22.  
Parsed in 0.003 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!

Last edited by 3m3r1c on Sat Sep 26, 2009 4:21 pm, edited 1 time in total.
3m3r1c
Freshman
Freshman
 
Posts: 9
Joined: Fri Sep 18, 2009 1:39 pm

Top

Postby Ferrat » Fri Sep 25, 2009 8:06 pm

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


There is your first problem stopping it from even running, you have to grab that exception, then you can find out why it doesn't work but as it says you are missing requestFeature() aswell to start with.
Hope that helps you on the way.

also a tip, when bug-hunting, always look at the first error, most errors after it stand a good chance to be just errors derived from the first one.
Ferrat
Freshman
Freshman
 
Posts: 4
Joined: Fri Sep 25, 2009 12:37 pm

Postby 3m3r1c » Fri Sep 25, 2009 8:56 pm

thank you for your answer I have locate the problem

in GraphicAppTest in onCreate if I comment the line

//requestWindowFeature(Window.FEATURE_NO_TITLE);

the program works. But I don't know why, and I want hidden the windows title

EDIT:

Ok that fine, I find my error. It was because I called requestWindowFeature(Window.FEATURE_NO_TITLE); after setContentView(R.layout.main);
3m3r1c
Freshman
Freshman
 
Posts: 9
Joined: Fri Sep 18, 2009 1:39 pm

Postby Ferrat » Fri Sep 25, 2009 9:42 pm

Looked at your code, just move the

requestWindowFeature(Window.FEATURE_NO_TITLE);

above the setContentView(R.layout.main);
and try that.

EDIT
Oh you did that, great =)
Ferrat
Freshman
Freshman
 
Posts: 4
Joined: Fri Sep 25, 2009 12:37 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests