Ok, donc j'ai juste commencé à essayer d'apprendre comment faire du codage hier avec Eclipse sur la plateforme android. J'ai codé beaucoup d'autres langues, mais jamais dans cet environnement. Je ne sais pas pourquoi cela a commencé ...Droid Erreur. (Newbie Coder)
Symptômes: L'application ne démarre pas dans l'émulateur.
Debugger Error: "Thread [<8> Thread-8] (Suspended (exception NullPointerException))" TutorialThread.run() line: 225
Cette ligne est un crochet de fermeture.
Variables:
this (TutorialThread (id=30067699104))
c (Surface$CompaitbleCanvas (id-830067697136))
Comme je le disais bien, je suis très nouveau à cela et j'avons suivi beaucoup de tutoriels et beaucoup de recherche sur Google. :/Cependant, je me tape la tête sur le clavier à ce sujet maintenant. Toute aide avec ceci est grandement appréciée.
package com.Joey_Ant.Lite;
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.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
public class JoeyAnt extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private TutorialThread _thread;
private int _x=20;
private int _y=20;
private int _x2=20;
private int _y2=20;
private int _mx=0;
private int _my=0;
private double _angle = 0;
private int _width=50;
private int _height=50;
private double _xchange=0;
private double _ychange=0;
private double _speed=5;
private int _stage=0;
private int _level=0;
private int _swidth=0;
private int _sheight=0;
private int _bx=0;
private int _b0w=0;
private int _b0h=0;
private int _b0y=0;
private int _b1w=0;
private int _b1h=0;
private int _b1y=0;
private int _b2w=0;
private int _b2h=0;
private int _b2y=0;
private int _b3w=0;
private int _b3h=0;
private int _b3y=0;
/**
* Stage 0 - Main Menu
* Stage 1 - Controls
* Stage 2 - Introduction
* Stage 3 - Game Over
* Stage 4 - Round Win
* Stage 5 - In Game
*
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry=false;
} catch (InterruptedException e) {
//Retry
}
}
}
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
_mx = (int) event.getX();
_my = (int) event.getY();
_x2 = _mx - (_width/2);
_y2 = _my - (_height/2);
if (_stage==0) {
if ((_mx > _bx)&&(_mx < _bx+_b0w)) {
// Within horizontal hit area.
if ((_my > _b0y)&&(_my < _b0y+_b0h)) {
// NEW GAME.
_stage=1;
}
}
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
if (_level==0) {
}
if (_stage==0) {
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.WHITE);
paint.setTextSize(24);
Bitmap _logo = BitmapFactory.decodeResource(getResources(), R.drawable.joeyant);
Bitmap _newgame = BitmapFactory.decodeResource(getResources(), R.drawable.new_game);
Bitmap _resumegame = BitmapFactory.decodeResource(getResources(), R.drawable.resume_game);
Bitmap _controls = BitmapFactory.decodeResource(getResources(), R.drawable.controls);
Bitmap _credits = BitmapFactory.decodeResource(getResources(), R.drawable.credits);
_swidth=canvas.getWidth();
_sheight=canvas.getHeight();
canvas.drawColor(Color.argb(255, 225, 199, 77));
canvas.drawBitmap(_logo, (_swidth/2)-(_logo.getWidth()/2), 20, null);
_bx=(_swidth/2)-(_newgame.getWidth()/2);
_b0y=150;
_b1y=210;
_b2y=270;
_b3y=330;
_b0w=_newgame.getWidth();
_b1w=_resumegame.getWidth();
_b2w=_controls.getWidth();
_b3w=_credits.getWidth();
_b0h=_newgame.getHeight();
_b1h=_resumegame.getHeight();
_b2h=_controls.getHeight();
_b3h=_credits.getHeight();
canvas.drawBitmap(_newgame, _bx, _b0y, null);
canvas.drawBitmap(_resumegame, _bx, _b1y, null);
canvas.drawBitmap(_controls, _bx, _b2y, null);
canvas.drawBitmap(_credits, _bx, _b3y, null);
}
if (_stage==1) {
// Introduction!
paint.setColor(Color.BLACK);
paint.setTextSize(18);
paint.setStyle(Paint.Style.STROKE);
Bitmap _intro = BitmapFactory.decodeResource(getResources(), R.drawable.introduction);
canvas.drawColor(Color.argb(255, 225, 199, 77));
canvas.drawBitmap(_intro, (_swidth/2)-(_intro.getWidth()/2), (_sheight/2)-(_intro.getHeight()), null);
}
if (_stage==2) {
paint.setColor(Color.WHITE);
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
canvas.drawColor(Color.argb(255, 225, 199, 77));
canvas.drawBitmap(_scratch, _x, _y, null);
canvas.drawText(String.valueOf(_x)+","+String.valueOf(_y)+" : "+String.valueOf(_x2)+","+String.valueOf(_y2), 20, 20, paint);
_width=_scratch.getWidth();
_height=_scratch.getHeight();
_angle = Math.atan2(_y2-_y, _x2-_x);
_xchange=Math.cos(_angle) * _speed;
_ychange=Math.sin(_angle) * _speed;
if (_x != _x2) {
_x+=_xchange;
}
if (Math.abs(_x2-_x)<_xchange) {
_x=_x2;
}
if (_y != _y2) {
_y+=_ychange;
}
if (Math.abs(_y2-_y)<_ychange) {
_y=_y2;
}
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(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) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
La ligne du pointeur NULL ne sera pas un crochet de fermeture. Assurez-vous que votre exécution de la dernière version de votre code lorsque vous produisez l'erreur, de sorte que vous obtenez des numéros de ligne précis – Blundell
Ok, cela n'a aucun sens pour moi. Maintenant, je suis vraiment po'd ... J'ai pensé que je prendrais une pause et y reviendrais ce matin. Je n'ai pas éteint mon ordinateur, je le laisse dormir. Je suis revenu et j'ai essayé maintenant et ça ne pose aucun problème ... Qu'est-ce que ... Il n'y a AUCUN changement de code quoi que ce soit ... Pourquoi le ferait-il? – Thomas
Vous avez peut-être exécuté une ancienne version de votre code. Si vous essayez d'exécuter votre application avant qu'elle ne soit compilée et construite (ce qui prend du temps, selon la taille de votre projet), vous utiliserez l'ancienne version de votre code. – Blundell