asa
asa

Reputation: 13

How to display sensor data values

Hi I am very new to android. I am trying to write a code that would collect sensor data. I tried the example given in the "http://developer.android.com/guide/topics/sensors/sensors_overview.html" Now I installed the apk file on my phone (Samsung galaxy S3) the app runs but crashes. This is the code that I have written:

The app crashes whenever I put the line "tv1.setText(Float.toString(lux));" in the onSensoeChanged() method. Kindly tell me what I am doing wrong here and how to correct the code.

This is my first post in this site. Apologies for any incorrect format of posting questions

Thanks

`

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);
      }
    }
`

Upvotes: 0

Views: 5215

Answers (3)

S.Ahsan
S.Ahsan

Reputation: 399

All your sensors should be initialized before the

setContentView(R.layout.activity_main);

Or your program will give a runtime error and crash down.

your Code may look something like this,

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)
     }     

Upvotes: 0

Shamm
Shamm

Reputation: 1004

Try tv1.setText(String.valueOf(lux)); instead of tv1.setText(Float.toString(lux));

Upvotes: 1

DeliriumTremens
DeliriumTremens

Reputation: 340

        runOnUiThread(new Runnable() {

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

Upvotes: 0

Related Questions