2013-02-18 80 views
0

我想使應用程序使用加速計,但是當我運行我的應用程序它說不幸的應用程序已停止。我有其他的代碼,但我刪除它,因爲我認爲這是搞亂了代碼,但事實並非如此。Android:不幸的應用程序已停止

下面是代碼

`

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Intent one = new Intent(getApplicationContext(), act.class); 
    startActivity(one); 


} 


     } 
      class act extends Activity implements SensorEventListener { 
       Intent one = getIntent(); 
       private SensorManager sensorManager; 
       private Sensor accelerometer; 
      float x,y,z; 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 

        setContentView(R.layout.activity2); 
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
      } 

      protected void onResume() { 
        super.onResume(); 
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); 
       } 
       protected void onPause() { 
        super.onPause(); 
        sensorManager.unregisterListener(this); 
       } 


      @Override 
      public void onAccuracyChanged(Sensor accelerometer, int accuracy) { 
      } 
      public void onSensorChanged(SensorEvent event) { 

        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 

         x = event.values[0]; 
         y = event.values[1]; 
         z = event.values[2]; 

        } 
         } 

      }` 

兩個類都在同一個文件

的logcat:

02-19 14:32:24.490: E/AndroidRuntime(7916): FATAL EXCEPTION: main 
02-19 14:32:24.490: E/AndroidRuntime(7916): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a3/com.example.a3.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.a3/com.example.a3.act}; have you declared this activity in your AndroidManifest.xml? 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.os.Looper.loop(Looper.java:137) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at dalvik.system.NativeStart.main(Native Method) 
02-19 14:32:24.490: E/AndroidRuntime(7916): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.a3/com.example.a3.act}; have you declared this activity in your AndroidManifest.xml? 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Activity.startActivityForResult(Activity.java:3370) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Activity.startActivityForResult(Activity.java:3331) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Activity.startActivity(Activity.java:3566) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Activity.startActivity(Activity.java:3534) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at com.example.a3.MainActivity.onCreate(MainActivity.java:32) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Activity.performCreate(Activity.java:5104) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
02-19 14:32:24.490: E/AndroidRuntime(7916):  ... 11 more 
+3

將您的logcat添加到您的文章。 – 2013-02-18 19:31:38

+3

在調用'onCreate()'之前要小心嘗試使用Context,例如:'Intent one = getIntent();'可能會導致錯誤。 – Sam 2013-02-18 19:33:41

+1

不止這些,它確實如此。在調用onCreate之前,上下文是無效的,我之前已經看到它導致錯誤。 – 2013-02-18 19:35:24

回答

0

機會是你沒有申報的行爲活動在清單中正確。在你的項目上運行android lint,看看它是否告訴你發生了什麼。或者在Eclipse中查看所有清單查看器選項卡中的清單文件(而不僅僅是代碼),然後查看它是否會給出紅色警告。

相關問題