2012-12-06 90 views
0

我想要統計android手機用戶使用的步數(基本上我想實現一個基本的計步器)。我使用的邏輯是,如果z方向的加速度變化大於某個thresold值,那麼它必須被計爲一步。使用加速計傳感器時Android應用程序崩潰

我的應用程序使用下面的代碼使用加速度計來計算步數取

package com.bhaskar.workout_helper_app; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

    public class CountActivity extends Activity implements SensorEventListener { 

private float mLastZ,mLastY,mLastX; 
private int stepcount; 
private TextView tv; 
private SensorManager mSensorManager; 
private Sensor mAccelerometer; 
private Boolean mInitialized; 

// constants 
private final float LEG_THRSHOLD_AMPLITUDE = (float) 5.0; 
private static final float NOISE = (float) 2.0; 

/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.countactivity); 

    // initialise variables 
    stepcount = 0; 
    tv = (TextView) findViewById(R.id.textView3); 
    mInitialized=false; 
    // initialise sensor manager and check whether accelerometer is present 
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
    mAccelerometer = mSensorManager 
      .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    if (mAccelerometer == null) { 
     Log.d("HARDWARE", "ACCELEROMETER NOT PRESENT!!!"); 
     startActivity(new Intent(CountActivity.this, workoutdata.class)); 
    } else { 
     if (mAccelerometer.getMinDelay() != 0) { 
      Log.d("ACCELEROMETER", 
        "reports data only when the value of accelerometer is changed!!!"); 
      //tv.setText("at regular interval "+SensorManager.SENSOR_DELAY_NORMAL); 
     } 
     mSensorManager.registerListener(this, mAccelerometer, 
       SensorManager.SENSOR_DELAY_NORMAL); 
    } 
    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      tv.setText(stepcount); 
      // TODO Auto-generated method stub 
      startActivity(new Intent(CountActivity.this, MainActivity.class)); 
     } 
    }); 
} 

@Override 
public void onAccuracyChanged(Sensor sensor, int accuracy) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onSensorChanged(SensorEvent event) { 
    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) { 
     return; 
    } 
    float x = event.values[0]; 
    float y = event.values[1]; 
    float z = event.values[2]; 


    if(Math.abs(z) > LEG_THRSHOLD_AMPLITUDE) 
     stepcount++; 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    if (mAccelerometer != null) 
     mSensorManager.registerListener(this, mAccelerometer, 
       SensorManager.SENSOR_DELAY_NORMAL); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    if (mAccelerometer != null) 
     mSensorManager.unregisterListener(this); 
} 
} 

但它崩潰每當我測試了我的電話,請幫助我該怎麼辦?

+0

不要忘記添加崩潰日誌。當應用程序崩潰! –

+0

它在哪裏崩潰?當你在調試器中運行它時,它會向你顯示出現異常的確切位置。 – Elmi

回答

0

使用本

tv.setText(""+stepcount); 

,而不是

 tv.setText(stepcount);