Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a number using Accelerometer value

I'm quite new to android so forgive me if I am asking a dead simple question.

Basically What I want to do is Display a number. when the value of Y, event.value[1] is between 1 and 7.

I can display values of X Y Z position. but as soon as I add if statement to check my app stops working (See code below)

I really need someones help please.

Thanks you for your help.

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub

float x = event.values[0];
float y = event.values[1];
float z = event.values[2];

int YValueMin = 1;
int YValueMax = 7;

accelero.setText("X: "+event.values[0]+
        "\nY: "+event.values[1]+
        "\nZ: "+event.values[2]);

if (y >= YValueMin && y <= YValueMax ){

    RepCounter.setText("1");

    }
else {
    RepCounter.setText(" ");
}
}
like image 526
user3198416 Avatar asked Dec 04 '25 08:12

user3198416


1 Answers

What does the logcat say when it crashes?

It is highly likely that your RepCounter is null, you can check this using the debugger.

Edit: Please change your code to the following, it could be a solution. If not, I will need the full source code to make sure RepCounter isn't a class or uninitialised object.

public void onSensorChanged(SensorEvent event)
    {
        if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        {
            float x = event.values[0]; 
            float y = event.values[1]; 
            float z = event.values[2];
            int YValueMin = 1; 
            int YValueMax = 7;

            accelero.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
            if(y >= YValueMin && y <= YValueMax)
            {
                RepCounter.setText("1");
            }
            else
            {
                RepCounter.setText(" ");
            }
        }
    }
like image 194
Remy Baratte Avatar answered Dec 05 '25 22:12

Remy Baratte