2012-11-02 106 views
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編碼)。

回答

3

您正在訪問padView設定值之前。這就是爲什麼你在Snake_Basic.<init>(Snake_Basic.java:21)中獲得NullPointerException,並且您的活動無法通過RuntimeException實例化。

在設置了padView之後,將所有類成員初始化設置移動到onCreate()

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進行更正。

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; 
    ... 
+1

+1。在'OnCreate'方法的第31行創建'padView'。 IOW,它首次被引用後約有10行,它實際上是構建的。 –