Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

java - Android app force closes when painting a ball?

I'm trying to paint a ball on the screen, but it force closes. Something is going wrong.

Ball b = new Ball();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ingame);
    start();
}

private void start() {
    Ball b = new Ball();
}

public void paint(Graphics g) {
    b.paint(g);
    g.setColor(Color.GREEN);
    g.fillOval(x-radius, y-radius, radius*2, radius*2);
}

Here's my LogCat:

10-15 21:02:45.492: E/dalvikvm(7847): Could not find class 'com.game.src.Ball', referenced from method com.jordan.bungee.bounce.Cracka. 10-15 21:02:45.492: W/dalvikvm(7847): VFY: unable to resolve new-instance 417 (Lcom/game/src/Ball;) in Lcom/jordan/bungee/bounce/Cracka; 10-15 21:02:45.492: D/dalvikvm(7847): VFY: replacing opcode 0x22 at 0x002b 10-15 21:02:45.492: D/dalvikvm(7847): VFY: dead code 0x002d-0032 in Lcom/jordan/bungee/bounce/Cracka;. ()V 10-15 21:02:45.496: E/dalvikvm(7847): Could not find class 'com.game.src.Ball', referenced from method com.jordan.bungee.bounce.Cracka.start 10-15 21:02:45.496: W/dalvikvm(7847): VFY: unable to resolve new-instance 417 (Lcom/game/src/Ball;) in Lcom/jordan/bungee/bounce/Cracka;

10-15 21:02:45.496: D/dalvikvm(7847): VFY: replacing opcode 0x22 at 0x0000

10-15 21:02:45.500: D/dalvikvm(7847): VFY: dead code 0x0002-0005 in Lcom/jordan/bungee/bounce/Cracka;.start ()V

10-15 21:02:45.500: W/dalvikvm(7847): VFY: unable to find class referenced in signature (Ljava/awt/Graphics;)

10-15 21:02:45.503: I/dalvikvm(7847): Could not find method com.game.src.Ball.paint, referenced from method com.jordan.bungee.bounce.Cracka.paint

10-15 21:02:45.503: W/dalvikvm(7847): VFY: unable to resolve virtual method 3021: Lcom/game/src/Ball;.paint (Ljava/awt/Graphics;)V

10-15 21:02:45.503: D/dalvikvm(7847): VFY: replacing opcode 0x6e at 0x0002

10-15 21:02:45.503: D/dalvikvm(7847): VFY: dead code 0x0005-001f in Lcom/jordan/bungee/bounce/Cracka;.paint (Ljava/awt/Graphics;)V

10-15 21:02:45.507: D/AndroidRuntime(7847): Shutting down VM

10-15 21:02:45.507: W/dalvikvm(7847): threadid=1: thread exiting with uncaught exception (group=0x4001e578) 10-15 21:02:45.527: E/AndroidRuntime(7847): FATAL EXCEPTION: main

10-15 21:02:45.527: E/AndroidRuntime(7847): java.lang.NoClassDefFoundError: com.game.src.Ball

10-15 21:02:45.527: E/AndroidRuntime(7847): at com.jordan.bungee.bounce.Cracka.(Cracka.java:22)

10-15 21:02:45.527: E/AndroidRuntime(7847): at java.lang.Class.newInstanceImpl(Native Method)

10-15 21:02:45.527: E/AndroidRuntime(7847): at java.lang.Class.newInstance(Class.java:1409)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.app.ActivityThread.access$1500(ActivityThread.java:117)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.os.Handler.dispatchMessage(Handler.java:99)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.os.Looper.loop(Looper.java:130)

10-15 21:02:45.527: E/AndroidRuntime(7847): at android.app.ActivityThread.main(ActivityThread.java:3687)

10-15 21:02:45.527: E/AndroidRuntime(7847): at java.lang.reflect.Method.invokeNative(Native Method)

10-15 21:02:45.527: E/AndroidRuntime(7847): at java.lang.reflect.Method.invoke(Method.java:507)

10-15 21:02:45.527: E/AndroidRuntime(7847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)

10-15 21:02:45.527: E/AndroidRuntime(7847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)

10-15 21:02:45.527: E/AndroidRuntime(7847): at dalvik.system.NativeStart.main(Native Method)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not sure why you're defining a local variable b in your start() method since you've already defined one as a class member. The one you created in start() will be lost as soon as start() returns.

Also: the member object you create will be instantiated long before onCreate() is called. Is there enough context available when the Activity is created for Ball() to be created successfully? Without seeing the source code to Ball() we can't tell.

Your logcat output indicates that the class for Ball was not found. Did you write it? Did you put it in the right directory where Eclipse could find it?

This question and your follow-ups seem to indicate that you're extremely new to Android programming, and possibly new to Java. If you're new to Java, I would think good and hard about trying to learn Android so soon. Android is not a programming environment for beginners.

That said, I suggest you start out by doing some of the tutorials to familiarize yourself with the system. Then, to write your own application, find a tutorial that most closely resembled what you're trying to do, copy it, and start making modifications. I almost always start new projects by copying old projects. This way, you know that filesystem layout and so forth is probably correct to begin it.

Finally, at the bottom of your Eclipse window, you'll see a tab labeled "Problems". This can be very useful for finding out what's gone wrong. In your case, the explanation to why Ball wasn't found is likely to be there. And of course, check your Console and Logcat tabs as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...