1 pièce(s) jointe(s)
Problème accès getResources() dans ma classe objet
Bonjour,
J'ai un message d'erreur dans mon application Android dans Eclipse avec getResources() dans ma classe Objet "Balle" :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 08-09 04:05:14.791: D/AndroidRuntime(1232): Shutting down VM
08-09 04:05:14.791: W/dalvikvm(1232): threadid=1: thread exiting with uncaught exception (group=0xb4a81ba8)
08-09 04:05:14.851: E/AndroidRuntime(1232): FATAL EXCEPTION: main
08-09 04:05:14.851: E/AndroidRuntime(1232): Process: com.example.balle, PID: 1232
08-09 04:05:14.851: E/AndroidRuntime(1232): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.balle/com.example.balle.MainActivity}: java.lang.NullPointerException
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.os.Handler.dispatchMessage(Handler.java:102)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.os.Looper.loop(Looper.java:136)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-09 04:05:14.851: E/AndroidRuntime(1232): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 04:05:14.851: E/AndroidRuntime(1232): at java.lang.reflect.Method.invoke(Method.java:515)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-09 04:05:14.851: E/AndroidRuntime(1232): at dalvik.system.NativeStart.main(Native Method)
08-09 04:05:14.851: E/AndroidRuntime(1232): Caused by: java.lang.NullPointerException
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.example.balle.Balle.<init>(Balle.java:15)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.example.balle.BalleView.<init>(BalleView.java:16)
08-09 04:05:14.851: E/AndroidRuntime(1232): at com.example.balle.MainActivity.onCreate(MainActivity.java:13)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.Activity.performCreate(Activity.java:5231)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-09 04:05:14.851: E/AndroidRuntime(1232): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
08-09 04:05:14.851: E/AndroidRuntime(1232): ... 11 more |
J'ai 3 fichiers java :
Balle.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.example.balle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
public class Balle extends Activity {
float radius = 75; // Rayon de la Balle
float x = radius + 20; // Centre de la Balle (x,y)
float y = radius + 40;
Context context = BalleView.GlobalContext;
Resources res = context.getResources();
// Constructeur
public Balle() {
}
public void draw(Canvas canvas) {
// affichage objet à partir d'une image
Bitmap monImage = BitmapFactory.decodeResource(res, R.drawable.bubble);
canvas.drawBitmap(monImage, x-radius, y-radius, null);
}
} |
BalleView.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.example.balle;
import com.example.balle.Balle;
import android.content.Context;
import android.graphics.Canvas;
import android.view.View;
public class BalleView extends View {
private Balle ball;
public static Context GlobalContext = null;
// Constructor
public BalleView(Context context) {
super(context);
ball = new Balle();
}
@Override
protected void onDraw(Canvas canvas) {
// dessine la balle
// affichage objet à partir d'une image
ball.draw(canvas);
}
} |
MainActivity.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.example.balle;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View BalleView = new BalleView(this);
setContentView(BalleView);
BalleView.setBackgroundColor(Color.WHITE);
}
} |
Voici également mon fichier image ressource dans res/drawable : Pièce jointe 576922
Je ne trouve pas sur internet une soluton à mon problème d'exécution.
Merci pour votre aide.