Android的加速度计来测量速度 [英] android accelerometer to measure speed

查看:213
本文介绍了Android的加速度计来测量速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用加速度计做一个测速仪,但由于某些原因,code崩溃是否有人可以告诉我什么是错的,如何解决 谢谢

这是在logcat中红色部分:

  05-14 13:03:55.826:信息/ ActivityManager(91):启动PROC com.hanud.speedo的活动com.hanud.speedo / .speedometer:PID = 2052 UID = 10127导报= {}
05-14 13:03:56.806:DEBUG / AndroidRuntime(2052):关闭虚拟机
05-14 13:03:56.806:WARN / dalvikvm(2052):主题ID = 1:螺纹退出与未捕获的异常(组= 0x400207f0)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):致命异常:主要
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):java.lang.RuntimeException的:无法实例活动ComponentInfo {com.hanud.speedo / com.hanud.speedo.speedometer}:抛出java.lang.ClassNotFoundException:COM .hanud.speedo.speedometer装载机dalvik.system.PathClassLoader [/data/app/com.hanud.speedo-2.apk]
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.os.Handler.dispatchMessage(Handler.java:99)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.os.Looper.loop(Looper.java:123)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.app.ActivityThread.main(ActivityThread.java:4627)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在java.lang.reflect.Method.invokeNative(本机方法)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在java.lang.reflect.Method.invoke(Method.java:521)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在dalvik.system.NativeStart.main(本机方法)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):抛出java.lang.ClassNotFoundException:产生的原因com.hanud.speedo.speedometer装载机dalvik.system.PathClassLoader [/data/app/com.hanud.speedo -2.apk]
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-14 13:03:56.986:ERROR / AndroidRuntime(2052):11 ...更多
05-14 13:03:57.017:WARN / ActivityManager(91):强制完成活动com.hanud.speedo / .speedometer
05-14 13:03:57.526:WARN / ActivityManager(91):活动暂停超时HistoryRecord {4382d820 com.hanud.speedo / .speedometer}
05-14 13:04:05.666:DEBUG / dalvikvm(1890年):GC_EXPLICIT释放4544对象/ 322792字节288ms
05-14 13:04:07.796:WARN / ActivityManager(91):活动破坏超时HistoryRecord {4382d820 com.hanud.speedo / .speedometer}
05-14 13:04:12.016:信息/ ActivityManager(91):启动PROC com.android.email服务com.android.email/.service.MailService:PID = 2058的uid = 10004导报= {3003,1015}
 

这是code:

 进口java.util.Date;
进口java.util.Timer中;
进口java.util.TimerTask中;

进口android.app.Activity;
进口android.content.Context;
进口android.hardware.Sensor;
进口android.hardware.SensorEvent;
进口android.hardware.SensorEventListener;
进口android.hardware.SensorManager;
进口android.os.Bundle;
进口android.os.Handler;
进口android.widget.TextView;

公共类里程表延伸活动{

  处理程序处理程序=新的处理程序();

  的SensorManager的SensorManager;
    TextView的myTextView;

    浮appliedAcceleration = 0;
    浮currentAcceleration = 0;
    浮动速度= 0;
    日期lastUpdate;

    @覆盖
    公共无效的onCreate(包冰柱){
      super.onCreate(冰柱);
      的setContentView(R.layout.main);

      myTextView =(TextView中)findViewById(R.id.myTextView);
      lastUpdate =新的日期(System.currentTimeMillis的());

      的SensorManager =(的SensorManager)getSystemService(Context.SENSOR_SERVICE);
      Sensor重力感应= sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener,
                                       加速度计,
                                       SensorManager.SENSOR_DELAY_FASTEST);


      定时器updateTimer =新的定时器(velocityUpdate);
      updateTimer.scheduleAtFixedRate(新的TimerTask(){
        公共无效的run(){
          updateGUI();
        }
      },0,1000);
    }

    私人无效updateGUI(){
      //从每秒米转换为英里每小时。
      最后的双英里=(Math.round(100 *速度/ 1.6 * 3.6))/ 100;

      //更新GUI
      handler.post(新的Runnable(){
      公共无效的run(){
          myTextView.setText(将String.valueOf(MPH)+英里);
      }
    });
    }

  私人无效updateVelocity(){
      //计算多久,这加速了应用。
      日期timeNow =新的日期(System.currentTimeMillis的());
      长timeDelta = timeNow.getTime() -  lastUpdate.getTime();
      lastUpdate.setTime(timeNow.getTime());

      //计算在速度变化
      //自上次更新当前的加速度。
      浮deltaVelocity = appliedAcceleration *(timeDelta / 1000);
      appliedAcceleration = currentAcceleration;

      //添加速度的变化,以目前的速度。
      速度+ = deltaVelocity;
    }

  //私人最终SensorListener sensorListener =新SensorListener(){
  私人最终SensorEventListener sensorEventListener =新SensorEventListener(){
    双标定= Double.NaN;



      公共无效onSensorChanged(SensorEvent事件){
        双X = event.values​​ [0];
        双Y = event.values​​ [1];
        双Z = event.values​​ [2];

      双一的Math.sqrt =(Math.pow(X,2)+ Math.pow(γ,2)+ Math.pow(Z,2));

      如果(校准== Double.NaN)
        校准=一个;
      其他 {
        updateVelocity();
        currentAcceleration =(浮点)一个;
      }
    }
      公共无效onAccuracyChanged(传感器传感器,诠释精度){}
  };
}
 

解决方案

您定义的

(很可能在Android清单) com.hanud.speedo.speedometer ,而你的活动的名称为车速(资本取值)。

只是要清楚 - 在清单文件更改活动的名称为车速表

im trying to make an speedometer using the accelerometer but for some reason the code is crashing can someone please tell me what is wrong and how to fix it thank you

this is the red part in logcat:

05-14 13:03:55.826: INFO/ActivityManager(91): Start proc com.hanud.speedo for activity com.hanud.speedo/.speedometer: pid=2052 uid=10127 gids={}
05-14 13:03:56.806: DEBUG/AndroidRuntime(2052): Shutting down VM
05-14 13:03:56.806: WARN/dalvikvm(2052): threadid=1: thread exiting with uncaught exception (group=0x400207f0)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052): FATAL EXCEPTION: main
05-14 13:03:56.986: ERROR/AndroidRuntime(2052): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hanud.speedo/com.hanud.speedo.speedometer}: java.lang.ClassNotFoundException: com.hanud.speedo.speedometer in loader dalvik.system.PathClassLoader[/data/app/com.hanud.speedo-2.apk]
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.os.Looper.loop(Looper.java:123)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at java.lang.reflect.Method.invoke(Method.java:521)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at dalvik.system.NativeStart.main(Native Method)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052): Caused by: java.lang.ClassNotFoundException: com.hanud.speedo.speedometer in loader dalvik.system.PathClassLoader[/data/app/com.hanud.speedo-2.apk]
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-14 13:03:56.986: ERROR/AndroidRuntime(2052):     ... 11 more
05-14 13:03:57.017: WARN/ActivityManager(91):   Force finishing activity com.hanud.speedo/.speedometer
05-14 13:03:57.526: WARN/ActivityManager(91): Activity pause timeout for HistoryRecord{4382d820 com.hanud.speedo/.speedometer}
05-14 13:04:05.666: DEBUG/dalvikvm(1890): GC_EXPLICIT freed 4544 objects / 322792 bytes in 288ms
05-14 13:04:07.796: WARN/ActivityManager(91): Activity destroy timeout for HistoryRecord{4382d820 com.hanud.speedo/.speedometer}
05-14 13:04:12.016: INFO/ActivityManager(91): Start proc com.android.email for service com.android.email/.service.MailService: pid=2058 uid=10004 gids={3003, 1015}

and this is the code:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class Speedometer extends Activity {

  Handler handler = new Handler();

  SensorManager sensorManager;
    TextView myTextView;

    float appliedAcceleration = 0;
    float currentAcceleration = 0;
    float velocity = 0;
    Date lastUpdate;    

    @Override
    public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);

      myTextView = (TextView)findViewById(R.id.myTextView);
      lastUpdate = new Date(System.currentTimeMillis());

      sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
      Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener,
                                       accelerometer,
                                       SensorManager.SENSOR_DELAY_FASTEST);


      Timer updateTimer = new Timer("velocityUpdate");
      updateTimer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
          updateGUI();
        }
      }, 0, 1000);
    }

    private void updateGUI() {
      // Convert from meters per second to miles per hour.
      final double mph = (Math.round(100*velocity / 1.6 * 3.6))/100;

      // Update the GUI
      handler.post(new Runnable() {
      public void run() {
          myTextView.setText(String.valueOf(mph) + "mph");
      }
    });
    }

  private void updateVelocity() {
      // Calculate how long this acceleration has been applied.
      Date timeNow = new Date(System.currentTimeMillis());
      long timeDelta = timeNow.getTime()-lastUpdate.getTime();
      lastUpdate.setTime(timeNow.getTime());

      // Calculate the change in velocity at the 
      // current acceleration since the last update. 
      float deltaVelocity = appliedAcceleration * (timeDelta/1000);
      appliedAcceleration = currentAcceleration;

      // Add the velocity change to the current velocity.
      velocity += deltaVelocity;
    }

  //private final SensorListener sensorListener = new SensorListener() {
  private final SensorEventListener sensorEventListener = new SensorEventListener() {
    double calibration = Double.NaN;



      public void onSensorChanged(SensorEvent event) {
        double x = event.values[0];
        double y = event.values[1];
        double z = event.values[2];

      double a = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); 

      if (calibration == Double.NaN)
        calibration = a;
      else {
        updateVelocity();
        currentAcceleration = (float)a;
      }
    }
      public void onAccuracyChanged(Sensor sensor, int accuracy) { }
  };
}

解决方案

You defined (probably in the android manifest) com.hanud.speedo.speedometer, while your activity's name is Speedometer (with capital S).

Just to make it clear - change the name of the activity in the Manifest file to Speedometer.

这篇关于Android的加速度计来测量速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆