0

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);
      }
    }
`
asa
  • 13
  • 1
  • 5

3 Answers3

1

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

Shamm
  • 1,004
  • 7
  • 9
  • Hi Shamm I tried tv1.setText(Float.toString(lux)); but the app still crashes. Whenever I try to reference the textview tv1.setText the app stops working ... Here is the xml code fragment_main.xml where I put the textview ... – asa Apr 05 '14 at 19:21
  • user3501857, why did you post code of fragment_main.xml? Your activity use 'setContentView(R.layout.activity_main);' By the way TextView should be inside of RelativeLayout tag. It will be better if you provide error log. – Shamm Apr 06 '14 at 08:55
  • sorry I wasn't sure what was going wrong ... anyways I have the app running now ... thanks ... I have one more query ... how can I make the app run in the background? I want collect sensor data for long periods of time ... I want to do something like after installing the app will run in the background and keep collecting sensor data ... how can I do that? – asa Apr 08 '14 at 20:27
  • Glad to hear you had succeeded! For your query this link can help you: http://stackoverflow.com/questions/6404186/how-can-i-make-my-android-app-run-on-the-background – Shamm Apr 09 '14 at 10:39
0
        runOnUiThread(new Runnable() {

        @Override
        public void run() {
            tv1.setText(Float.toString(lux));
        }
    });
DeliriumTremens
  • 340
  • 5
  • 14
0

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)
     }     
S.Ahsan
  • 389
  • 3
  • 15