1
回答
android输出重力感应的值,程序没问题,模拟后就出现意外然后报错了
【腾讯云】校园拼团福利,1核2G服务器10元/月!>>>   
我写了个重力感应的小软件,就是输出重力感应的值,程序没问题,模拟后就出现意外然后报错了
package android.ma_4;

import android.app.Activity;
import android.ma_4.R;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class Android_4Activity extends Activity {
    /** Called when the activity is first created. */
private float x,y,z;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
        final EditText Etext1=(EditText) findViewById(R.id.editText1);
     final int ROUND_NUMBER=100;
     SensorManager sensorMgr=(SensorManager) getSystemService(SENSOR_SERVICE);
     Sensor sensor=sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     {
     if(sensor==null)
     {
     Log.w("NO_SERVICE","没有重力感应装置");
     }
     else
     {
     SensorEventListener lsn=new SensorEventListener(){

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

     public void onSensorChanged(SensorEvent event) {
     // TODO Auto-generated method stub
      x=event.values[SensorManager.DATA_X];
      y=event.values[SensorManager.DATA_Y];
      z=event.values[SensorManager.DATA_Z];
     Etext1.setText("x="+Math.round(x*ROUND_NUMBER)
     +"y="+Math.round(y*ROUND_NUMBER)
     +"z="+Math.round(z*ROUND_NUMBER));
     }
             };
             sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
     }
     }
    }
} 
 
举报
林晓盛
发帖于6年前 1回/853阅
顶部