您的活动是否仍在运行/ Accelerometer?

时间:2014-01-29 09:18:21

标签: android accelerometer

我正在使用此程序代码来计算否。用户使用设备加速度计完成仰卧起坐(仰卧起坐)。它运行正常,但有一段时间它崩溃错误'你的活动仍在运行'。

以下是我的代码:

public class SessionActivity extends Activity implements SensorEventListener,OnClickListener {
    SensorManager sManager;


TextView tvCount;

enum CrunchStatus {
    PEAK_REACHED, PEAK_NOT_REACHED
}

CrunchStatus crStatus;
int counter = 0;
WakeLock wL;
Button btDone,btCancelSess;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.session);

    tvCount = (TextView) findViewById(R.id.tvCount);        
    crStatus = CrunchStatus.PEAK_NOT_REACHED;

    btDone=(Button) findViewById(R.id.btDone);      
    btDone.setOnClickListener(this);

    btCancelSess=(Button) findViewById(R.id.btQuitSess);        
    btCancelSess.setOnClickListener(this);      

    sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sManager.registerListener(this,sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);


    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wL = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
}

@Override
protected void onPause() {
    super.onPause();
    wL.release();
    sManager.unregisterListener(this);

}

@Override
protected void onResume() {
    super.onResume();
    wL.acquire();
    sManager.registerListener(this,sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);             
}


@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

@Override
public void onSensorChanged(SensorEvent event) {
    double z;
    z=event.values[2];

    if(z<1.5){
        crStatus=CrunchStatus.PEAK_REACHED;
    }
    else if(z>1.5 && crStatus==CrunchStatus.PEAK_REACHED){
        crStatus=CrunchStatus.PEAK_NOT_REACHED;
        counter++;          
        tvCount.setText(""+counter);

    }

}

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.btDone:
        loadSaveDialog();           
        break;
    case R.id.btQuitSess:
        loadQuitDialog();           
        break;
    }
}



private void loadSaveDialog() {
    final Dialog dialog=new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
    dialog.setContentView(R.layout.dialog_quit);


    ImageView ivQuitYes=(ImageView) dialog.findViewById(R.id.ivQuitYes);
    ivQuitYes.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            updateDatabase();
            (SessionActivity.this).finish();
        };
    });

    ImageView ivQuitCancel=(ImageView) dialog.findViewById(R.id.ivQuitCancel);
    ivQuitCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        dialog.dismiss();   
        }
    });

    dialog.show();

}


private void loadQuitDialog() {
    final Dialog dialog=new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);
    dialog.setContentView(R.layout.dialog_quit);

    ImageView ivQuitYes=(ImageView) dialog.findViewById(R.id.ivQuitYes);
    ivQuitYes.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            (SessionActivity.this).finish();
        };
    });

    ImageView ivQuitCancel=(ImageView) dialog.findViewById(R.id.ivQuitCancel);
    ivQuitCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        dialog.dismiss();   
        }
    });

    dialog.show();
}   
}

以下是错误日志:

android.view.WindowManager$BadTokenException: Unable to add window -- 
token android.os.BinderProxy@41ab0c78 
is not valid;is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:513)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
at android.view.Window$LocalWindowManager.addView(Window.java:537)
at android.app.Dialog.show(Dialog.java:278)
at com.bigdreams.situps.TrainingActivity.loadTimerDialog(TrainingActivity.java:327)
at com.bigdreams.situps.TrainingActivity.onSensorChanged(TrainingActivity.java:223)
at android.hardware.SensorManager$ListenerDelegate$1.handleMessage
(SensorManager.java:580)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4456)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案