1

still new at this ... when trying run my app, I just get a "error" and it closes. If I disable the "line" to display the sensor value, it run without a problem. Please assist me!!

public class MainActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;


    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;


    //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    TextView baroText;

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

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

        //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
        uiVars();
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);

        sensorManager.registerListener(new MyListener(), sensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void uiVars(){
        baroText = (TextView) findViewById(R.id.zSensorInfo2);
        //timeText = (TextView) findViewById(R.id.timeTxt);
        //refresh = (Button) findViewById(R.id.baroRefreshBtn);
    }

    class MyListener implements SensorEventListener {
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            //long timestamp = event.timestamp;
            float value = event.values[0];
            // do something with the values
            String valueString = String.valueOf(value);
            //baroText.setText(valueString);
        }
    }
}
yole
  • 92,896
  • 20
  • 260
  • 197
  • 1
    What line are you referring to and what is the error that you are getting? – mray190 May 26 '15 at 13:03
  • Use LogCat to examine the Java stack trace associated with your error: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare May 26 '15 at 13:07

1 Answers1

0

Without logcat results or any additional details, it is hard to figure out what your error is. For better help, please post debugging information next time.

Based on what I can see from looking at the code briefly, you may be having trouble with this line?

String valueString = String.valueOf(value);
//baroText.setText(valueString);

Try changing it to:

String valueString = Float.toString(value);
baroText.setText(valueString);

The other issue is that your sensor value may not be returning a value at all. So perhaps check to see if value is null first? If this is the case, valueString is trying to convert a null float value into a string which would error out

mray190
  • 496
  • 3
  • 13
  • Thank you for assisting me. I put the // in front of that line because removing it gives me a error, that is how I know where the problem is.Changed the source and added a "if statement". When I run the app, I am still getting a error. Let me know how/what you need for the "debugging information". `@Override public void onSensorChanged(SensorEvent event) { float value = event.values[0]; String valueString = Float.toString(value); if (valueString != null){ baroText.setText(valueString); } }` – Willie Botha May 27 '15 at 05:15
  • This is the line you should be looking at in the error log: `Pending exception java.lang.NullPointerException thrown by 'unknown throw location' java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at void za.co.litedata.orchidcare.MainActivity$MyListener.onSensorChanged(android.hardware.SensorEvent) (MainActivity.java:81) at void android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(int, float[], int, long) (SystemSensorManager.java:503)` – mray190 May 27 '15 at 11:32
  • I would suggest putting the if statement BEFORE the Float.toString method line. My guess is your sensors aren't working – mray190 May 27 '15 at 11:38