2015-10-07 106 views
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 
+0

呼叫 “btnView =新ButtonViews();”因为在创建ButtonViews对象时没有视图存在 –

+0

在第49行的'NullPointerException'我想这是当你试图分配一个监听器给你没有正确实例化的按钮时(参见Tauqir的答案) – PPartisan

回答

0
替换

btnView = new ButtonViews(); 
setContentView(R.layout.activity_main); 

随着

setContentView(R.layout.activity_main); 
btnView = new ButtonViews(); 
+0

谢谢朋友。它正在工作。 –