メニュー画面からアクティビティを起動するたびに、「致命的な例外:メイン」が発生します。他のいくつかのエラーとともに、ここにLogCatがあります
11-01 20:02:22.095: E/AndroidRuntime(272): FATAL EXCEPTION: main
11-01 20:02:22.095: E/AndroidRuntime(272): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{the.Newboston/the.Newboston.Snake_Basic}: java.lang.NullPointerException
11-01 20:02:22.095: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.os.Handler.dispatchMessage(Handler.java:99)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.os.Looper.loop(Looper.java:123)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-01 20:02:22.095: E/AndroidRuntime(272): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 20:02:22.095: E/AndroidRuntime(272): at java.lang.reflect.Method.invoke(Method.java:521)
11-01 20:02:22.095: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-01 20:02:22.095: E/AndroidRuntime(272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-01 20:02:22.095: E/AndroidRuntime(272): at dalvik.system.NativeStart.main(Native Method)
11-01 20:02:22.095: E/AndroidRuntime(272): Caused by: java.lang.NullPointerException
11-01 20:02:22.095: E/AndroidRuntime(272): at the.Newboston.Snake_Basic.<init>(Snake_Basic.java:21)
11-01 20:02:22.095: E/AndroidRuntime(272): at java.lang.Class.newInstanceImpl(Native Method)
11-01 20:02:22.095: E/AndroidRuntime(272): at java.lang.Class.newInstance(Class.java:1429)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
11-01 20:02:22.095: E/AndroidRuntime(272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
11-01 20:02:22.095: E/AndroidRuntime(272): ... 11 more
私のコードは
package the.Newboston;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class Snake_Basic extends Activity implements OnTouchListener {
SnakeView padView;
float x, y;
Bitmap up_pad, down_pad, left_pad, right_pad, static_pad_temp, static_pad;
boolean isRunning = false;
int screen_width = padView.getWidth();
int screen_height = padView.getHeight();
int snake_screen_width = (screen_width / 4);
int d_pad_width = snake_screen_width;
int d_pad_height = d_pad_width;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
padView = new SnakeView(this);
padView.setOnTouchListener(this);
x = 0;
y = 0;
up_pad = BitmapFactory.decodeResource(getResources(),
R.drawable.control_pad_up);
down_pad = BitmapFactory.decodeResource(getResources(),
R.drawable.control_pad_down);
left_pad = BitmapFactory.decodeResource(getResources(),
R.drawable.control_pad_left);
right_pad = BitmapFactory.decodeResource(getResources(),
R.drawable.control_pad_right);
static_pad_temp = BitmapFactory.decodeResource(getResources(),
R.drawable.control_pad_neutral);
static_pad = Bitmap.createScaledBitmap(static_pad_temp, d_pad_width,
d_pad_height, true);
setContentView(padView);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
padView.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
padView.resume();
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
return true;
}
public class SnakeView extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
int string_creator = 0;
Thread ourThread = null;
boolean isRunning = false;
public SnakeView(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause() {
isRunning = false;
while (true) {
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume() {
ourThread = new Thread(this);
ourThread.start();
isRunning = true;
}
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);
if (x != 0 && y != 0) {
canvas.drawBitmap(static_pad, x
- (static_pad.getWidth() / 2),
y - (static_pad.getHeight() / 2), null);
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}
誰かがエラーを見つけるためにふるいにかけることが多すぎるかどうかは完全に理解しています。私は初心者なので、おそらく初心者の間違いでしょう。ちなみに、実行前にエラーは発生していません(Eclipseでコーディングしています)。