简体   繁体   English

Android:不幸的是应用已停止

[英]Android: Unfortunately app has stopped

I am trying make an app that uses the accelerometer but when I run my app it says unfortunately app has stopped. 我正在尝试制作一个使用加速度计的应用程序,但是运行我的应用程序时,不幸的是该应用程序已停止。 I had other code but i deleted it because i thought it was messing up the code but it wasn't. 我有其他代码,但是我删除了它,因为我认为它弄乱了代码,但事实并非如此。

Here is the code 这是代码

` `

@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];

                    }
                        }

             }`

Both of the classes are in the same file 这两个类都在同一个文件中

LogCat: 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

Chances are you did not declare the act activity correctly in the manifest. 您可能没有在清单中正确声明行为活动。 Run android lint on your project, and see if it tells you what's happening. 在您的项目上运行android lint,看看它是否告诉您正在发生的事情。 Or in Eclipse look at your manifest file in all the manifest viewer tabs (not just code) and see if it gives you red warnings. 或者在Eclipse中,在所有清单查看器选项卡中查看清单文件(不仅仅是代码),然后查看它是否为您提供红色警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM