編集:LogCatを調べたところ、com.example.playgroundを膨らませることができなかったとのことでした。その後、com.game.myapp.Playgroundが必要であることに気づきました。変更してから動作しました。
最近、Androidアプリ(リンク)で重力が機能しない理由を尋ねましたが、それでも問題が発生します。ビューをクラス「Playground」に変更しましたが、強制終了します。私は何が間違っているのですか?
package com.game.myapp;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
public class InGame extends Activity{
Playground v;
private int radius;
private int xPosition;
private int yPosition;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Rewrite this, it sucks. Seriously.
super.onCreate(savedInstanceState);
v = new Playground(this);
setContentView(v);
}
public InGame(int x, int y, int radius, int color)
{
this.xPosition = x; this.yPosition = y; this.radius = radius;
paint = new Paint(color);
}
void moveBall(int x, int y){
xPosition = x; yPosition =y;
}
void onDraw(Canvas canvas){
canvas.drawCircle(xPosition, yPosition, radius, paint);
}
}
遊び場クラス:
package com.game.myapp;
import android.content.Context;
import android.graphics.Canvas;
import android.view.View;
public class Playground extends View{
public static InGame ball;
public Playground(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (ball != null ){
ball.onDraw(canvas);
}
}
}
LogCatは次のとおりです。
11-04 16:36:33.945: D/dalvikvm(13177): newInstance failed: no <init>()
11-04 16:36:33.949: D/AndroidRuntime(13177): Shutting down VM
11-04 16:36:33.949: W/dalvikvm(13177): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
11-04 16:36:33.953: E/AndroidRuntime(13177): FATAL EXCEPTION: main
11-04 16:36:33.953: E/AndroidRuntime(13177): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.game.myapp/com.game.myapp.InGame}: java.lang.InstantiationException: com.game.myapp.InGame
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.os.Looper.loop(Looper.java:130)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-04 16:36:33.953: E/AndroidRuntime(13177): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 16:36:33.953: E/AndroidRuntime(13177): at java.lang.reflect.Method.invoke(Method.java:507)
11-04 16:36:33.953: E/AndroidRuntime(13177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
11-04 16:36:33.953: E/AndroidRuntime(13177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-04 16:36:33.953: E/AndroidRuntime(13177): at dalvik.system.NativeStart.main(Native Method)
11-04 16:36:33.953: E/AndroidRuntime(13177): Caused by: java.lang.InstantiationException: com.game.myapp.InGame
11-04 16:36:33.953: E/AndroidRuntime(13177): at java.lang.Class.newInstanceImpl(Native Method)
11-04 16:36:33.953: E/AndroidRuntime(13177): at java.lang.Class.newInstance(Class.java:1409)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-04 16:36:33.953: E/AndroidRuntime(13177): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
11-04 16:36:33.953: E/AndroidRuntime(13177): ... 11 more