0
我寫了下面的一小段程序,但是當我執行這個程序時,它顯示錯誤。我提到了該程序的錯誤。 請,任何人都可以檢查和指導我什麼,哪裏是錯誤。提前致謝。我需要你的大力幫助。不顯示仿真器上的輸出
public class MainActivity extends Activity implements SensorEventListener, OnClickListener{
private boolean accelerometer = false; // boolean variable to turn accelerometer if button clicked
private Landing landing; //variable of Landing class to specify game area
private ButtonViews btnView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnView = new ButtonViews();
// requestWindowFeature(Window.FEATURE_NO_TITLE); // It won't display the heading with title
setContentView(R.layout.activity_main);// to get the content properties to be viewed on the screen once running
// landing = (Landing)findViewById(R.id.Landing);
// To control the accelerometer
/* SensorManager sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
*/
//Button Click listener for restart the game
btnView.btnRestart.setOnClickListener(this);
/* btnView.btnExit.setOnClickListener(this);
btnView.tilt.setOnClickListener(this);*/
}
class ButtonViews
{
public final Button btnRestart; //button to restart the game again
private final Button btnExit; // button to exit the game
private final Button btnLeft; // button to move the space craft right side turn left thruster
private final Button btnRight; // button to move the space craft left side turn right thruster
private final Button btnUp; // button to move the space craft up side turn main flame thruster
private final Button tilt; // button to activate accelerometer feature in the contorl
public ButtonViews(){
btnRestart = (Button)findViewById(R.id.btnstart);
btnExit = (Button)findViewById(R.id.btnExit);
btnRight = (Button)findViewById(R.id.btnRight);
btnLeft = (Button)findViewById(R.id.btnLeft);
btnUp = (Button)findViewById(R.id.btnUp);
tilt = (Button)findViewById(R.id.action_Accelerometer);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
10-07 20:18:02.908: D/dalvikvm(1805): GC_FOR_ALLOC freed 51K, 9% free 7990K/8707K, paused 11ms, total 11ms
10-07 20:18:02.916: I/dalvikvm-heap(1805): Grow heap (frag case) to 9.206MB for 947964-byte allocation
10-07 20:18:02.932: D/dalvikvm(1805): GC_CONCURRENT freed <1K, 8% free 8916K/9671K, paused 11ms+1ms, total 16ms
10-07 20:18:02.992: D/dalvikvm(1805): GC_FOR_ALLOC freed 18K, 6% free 9113K/9671K, paused 6ms, total 6ms
10-07 20:18:02.996: I/dalvikvm-heap(1805): Grow heap (frag case) to 10.770MB for 1440012-byte allocation
10-07 20:18:03.012: D/dalvikvm(1805): GC_CONCURRENT freed <1K, 6% free 10518K/11079K, paused 12ms+1ms, total 15ms
10-07 20:18:03.068: D/dalvikvm(1805): GC_FOR_ALLOC freed <1K, 6% free 11035K/11719K, paused 4ms, total 5ms
10-07 20:18:03.096: D/AndroidRuntime(1805): Shutting down VM
10-07 20:18:03.096: W/dalvikvm(1805): threadid=1: thread exiting with uncaught exception (group=0xa625d288)
10-07 20:18:03.100: E/AndroidRuntime(1805): FATAL EXCEPTION: main
10-07 20:18:03.100: E/AndroidRuntime(1805): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.likhit.spacelanding/com.likhit.spacelanding.MainActivity}: java.lang.NullPointerException
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.os.Looper.loop(Looper.java:137)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-07 20:18:03.100: E/AndroidRuntime(1805): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 20:18:03.100: E/AndroidRuntime(1805): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 20:18:03.100: E/AndroidRuntime(1805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-07 20:18:03.100: E/AndroidRuntime(1805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-07 20:18:03.100: E/AndroidRuntime(1805): at dalvik.system.NativeStart.main(Native Method)
10-07 20:18:03.100: E/AndroidRuntime(1805): Caused by: java.lang.NullPointerException
10-07 20:18:03.100: E/AndroidRuntime(1805): at com.likhit.spacelanding.MainActivity.onCreate(MainActivity.java:49)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.Activity.performCreate(Activity.java:5008)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-07 20:18:03.100: E/AndroidRuntime(1805): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-07 20:18:03.100: E/AndroidRuntime(1805): ... 11 more
10-07 20:23:23.544: D/dalvikvm(1869): GC_FOR_ALLOC freed 54K, 9% free 7990K/8707K, paused 5ms, total 5ms
10-07 20:23:23.544: I/dalvikvm-heap(1869): Grow heap (frag case) to 9.206MB for 947964-byte allocation
10-07 20:23:23.636: D/dalvikvm(1869): GC_CONCURRENT freed <1K, 8% free 8916K/9671K, paused 68ms+1ms, total 90ms
10-07 20:23:23.704: D/dalvikvm(1869): GC_FOR_ALLOC freed 18K, 6% free 9113K/9671K, paused 8ms, total 8ms
10-07 20:23:23.704: I/dalvikvm-heap(1869): Grow heap (frag case) to 10.770MB for 1440012-byte allocation
10-07 20:23:23.720: D/dalvikvm(1869): GC_CONCURRENT freed <1K, 6% free 10518K/11079K, paused 11ms+1ms, total 15ms
10-07 20:23:23.788: D/dalvikvm(1869): GC_FOR_ALLOC freed <1K, 6% free 11035K/11719K, paused 3ms, total 4ms
10-07 20:23:23.800: D/AndroidRuntime(1869): Shutting down VM
10-07 20:23:23.808: W/dalvikvm(1869): threadid=1: thread exiting with uncaught exception (group=0xa625d288)
10-07 20:23:23.812: E/AndroidRuntime(1869): FATAL EXCEPTION: main
10-07 20:23:23.812: E/AndroidRuntime(1869): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.likhit.spacelanding/com.likhit.spacelanding.MainActivity}: java.lang.NullPointerException
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.os.Looper.loop(Looper.java:137)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-07 20:23:23.812: E/AndroidRuntime(1869): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 20:23:23.812: E/AndroidRuntime(1869): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 20:23:23.812: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-07 20:23:23.812: E/AndroidRuntime(1869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-07 20:23:23.812: E/AndroidRuntime(1869): at dalvik.system.NativeStart.main(Native Method)
10-07 20:23:23.812: E/AndroidRuntime(1869): Caused by: java.lang.NullPointerException
10-07 20:23:23.812: E/AndroidRuntime(1869): at com.likhit.spacelanding.MainActivity.onCreate(MainActivity.java:49)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.Activity.performCreate(Activity.java:5008)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-07 20:23:23.812: E/AndroidRuntime(1869): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-07 20:23:23.812: E/AndroidRuntime(1869): ... 11 more
呼叫 「btnView =新ButtonViews();」因爲在創建ButtonViews對象時沒有視圖存在 –
在第49行的'NullPointerException'我想這是當你試圖分配一個監聽器給你沒有正確實例化的按鈕時(參見Tauqir的答案) – PPartisan