2

每当我从菜单屏幕上启动我的活动时,我都会遇到“致命例外:主要”。除了一些其他错误,这里是 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 中编码)。

4

3 回答 3

3

您在padView设置其值之前访问。这就是为什么您进入NullPointerException并且Snake_Basic.<init>(Snake_Basic.java:21)您的活动无法使用RuntimeException.

将所有类成员初始化移入onCreate(), afterpadView设置。

于 2012-11-02T00:48:50.167 回答
1

请从Snake_Basic下面的类中查看这些行:

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();

您没有padView在第一行初始化并在最后一行使用它padView.getWidth();,这是导致NullPointerException. padView请通过在上面的第一行中初始化来更正。

于 2012-11-02T00:48:29.877 回答
0

1)感谢您的回溯,以及相应的代码。两者都是无价的。

2)回溯说问题是“Snake_Basic.java”第21行的“空指针异常”:

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)

3) 倒数到第 21 行,看起来“padView.getWidth()”正在返回一个“null”……当你尝试引用它时它就快死了。我怀疑“padView.getHeight()”可能也会失败,如果你的程序走得那么远。

问:您在哪里创建“padView”的实例?你确定它真的成功了吗?

确保在成功创建之前不要尝试使用“padView” !

============ 建议更改 =============

public class Snake_Basic extends Activity implements OnTouchListener {

  // These members are implicitly initialized to "0" (numbers) or "null" (objects)
  // *DON'T* try to initialize any objects here
  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, screen_height;
  ...
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    padView = new SnakeView(this);

    // Initialize stuff here, in your onCreate() method
    screen_width = padView.getWidth();
    screen_height = padView.getHeight();
    snake_screen_width = (screen_width / 4);
    d_pad_width = snake_screen_width;
    ...
于 2012-11-02T00:48:36.410 回答