Sucky Posted May 15, 2010 Share Posted May 15, 2010 Bonjour tout le monde ! Voila mon problème est simple mais très perturbant. J'ai un thread qui se charge de rafraichir mon graphics toute les 20 ms, et lorsque je quitte l'application, je crois que ce thread plante car je ne le quitte certainement pas bien, et j'ai donc un message d'erreure qui s'affiche de type "Fermeture soudaine de l'application" :/ Voici mon code : package mApp.pkg; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.SurfaceHolder; import android.view.View.OnTouchListener; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.BitmapFactory; import android.content.Context; public class test extends Activity { GamePlay tt; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tt=new GamePlay(this); setContentView(tt); } public boolean onKeyDown (int keyCode, KeyEvent event){ tt.onKeyDown(keyCode, event); Log.e("Touche",new Integer(keyCode).toString()); return super.onKeyDown(keyCode, event); } public boolean onOptionsItemSelected (MenuItem item){ Log.e("Menu",item.getTitle().toString()); return super. onOptionsItemSelected (item); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); //Creation du menu "Rejouer" return true; } public class GamePlay extends SurfaceView implements SurfaceHolder.Callback ,OnTouchListener { SurfaceHolder mSurfaceHolder; Bitmap fond; monTimer ttu=new monTimer(); public GamePlay(Context context) { super(context); fond=BitmapFactory.decodeResource(context.getResources(), R.drawable.back); SurfaceHolder holder = getHolder(); holder.addCallback(this); mSurfaceHolder=holder; setOnTouchListener(this); } private void doDraw(Canvas canvas) { canvas.save(); canvas.drawBitmap(fond, -50, 0, null); canvas.restore(); } //@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } //@Override public void surfaceCreated(SurfaceHolder holder) { ttu.start(); } //@Override public void surfaceDestroyed(SurfaceHolder holder) { } public boolean onKeyDown (int keyCode, KeyEvent event){ Log.e("keyCode",(new Integer(keyCode)).toString()); return true; } //@Override public boolean onTouch(View v, MotionEvent event) { return true; } class monTimer extends Thread implements Runnable { public void run() { while(true){ try { sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Canvas c = null; try{ c = mSurfaceHolder.lockCanvas(); doDraw(c); } finally{ if (c!=null) { mSurfaceHolder.unlockCanvasAndPost(c); } } } } } } } Et voila le message d'erreur dans le log : 05-15 17:21:58.653: WARN/dalvikvm(3318): threadid=13: thread exiting with uncaught exception (group=0x4001b390) 05-15 17:21:58.653: ERROR/AndroidRuntime(3318): Uncaught handler: thread Thread-7 exiting due to uncaught exception 05-15 17:21:58.653: ERROR/AndroidRuntime(3318): java.lang.NullPointerException 05-15 17:21:58.653: ERROR/AndroidRuntime(3318): at mApp.pkg.test$GamePlay.doDraw(test.java:68) 05-15 17:21:58.653: ERROR/AndroidRuntime(3318): at mApp.pkg.test$GamePlay.access$0(test.java:67) 05-15 17:21:58.653: ERROR/AndroidRuntime(3318): at mApp.pkg.test$GamePlay$monTimer.run(test.java:126) J'ai essayer de stop le thread avant de quitter, mais je n'ai trouver aucune manière propre de le faire. >< Link to comment Share on other sites More sharing options...
Burn2 Posted May 15, 2010 Share Posted May 15, 2010 En fait à cause de ton while true il est impossible de le stopper proprement. Fait donc un while (!stop) stop étant un booléen que tu passes à vrais lors ce que tu quittes ton application. et juste après (enfin si ça existe en java) tu fais un join() pour attendre que ton thread soit terminé. Sinon tu peux faire moins propre et killer le thread lorsque tu quittes mais bon là c'est bourrin je trouve, il faut éviter les while true. EDIT: visiblement il n'y a plus de méthode bourine: http://developer.android.com/reference/java/lang/Thread.html Elles sont toutes hasbeen. Link to comment Share on other sites More sharing options...
Sucky Posted May 15, 2010 Author Share Posted May 15, 2010 Génial !!! Ça fonctionne parfaitement ! Merci beaucoup =) Merci pour cette réponse rapide ! Je passe le thread en RESOLU =D Link to comment Share on other sites More sharing options...
Burn2 Posted May 15, 2010 Share Posted May 15, 2010 De rien bonne continuation. ;) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.