如何显示传感器数据值

时间:2014-04-05 18:16:37

标签: android

嗨,我对android很新。我正在尝试编写一个可以收集传感器数据的代码。我尝试了“http://developer.android.com/guide/topics/sensors/sensors_overview.html”中给出的示例现在我在手机上安装了apk文件(三星galaxy S3)应用程序运行但崩溃了。这是我写的代码:

每当我放入“tv1.setText(Float.toString(lux))”行时应用程序崩溃;“在onSensoeChanged()方法中。请告诉我我在这里做错了什么以及如何纠正代码。

这是我在本网站的第一篇文章。对任何错误的发布问题格式表示歉意

谢谢

`

package com.example.sensordata;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
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.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity implements SensorEventListener {

      public SensorManager mSensorManager;
      public Sensor mLight;

      public TextView tv1;
      //public TextView tv2;

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

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

        tv1 = (TextView) findViewById(R.id.text1);
        //tv2 = (TextView) findViewById(R.id.text2);    

      }

      @Override
      public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
      }

      @Override
      public final void onSensorChanged(SensorEvent event) {
        // The light sensor returns a single value.
        // Many sensors return 3 values, one for each axis.
          synchronized (this) {
              float lux = event.values[0];
                // Do something with this sensor value.
                //tv1 = (TextView) findViewById(R.id.text1);
              tv1.setText(Float.toString(lux));

          }

      }

      @Override
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);

      }

      @Override
      protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
      }
    }
`

3 个答案:

答案 0 :(得分:1)

尝试使用tv1.setText(String.valueOf(lux));代替tv1.setText(Float.toString(lux));

答案 1 :(得分:0)

        runOnUiThread(new Runnable() {

        @Override
        public void run() {
            tv1.setText(Float.toString(lux));
        }
    });

答案 2 :(得分:0)

所有传感器都应在

之前初始化
setContentView(R.layout.activity_main);

或者您的程序会发出运行时错误并崩溃。

您的代码可能看起来像这样,

public class MainActivity extends Activity implements SensorEventListener {

  public SensorManager mSensorManager;
  public Sensor mLight;

  public TextView tv1;
  //public TextView tv2;

  @Override
  public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initializesensor();

    setContentView(R.layout.activity_main);

     tv1 = (TextView) findViewById(R.id.text1);

     }

     public void initializesensor(){
     //write all sensor code here(excluding sensor stopping method)
     }