简体   繁体   中英

How to set a speed limit for a speedometer Android app?

For my regions science fair, I am making a speedometer app for Android and I want to set a speed limit. Here is the code I'm having trouble with:

     public void onLocationChanged(Location location) {
    TextView txt = (TextView) this.findViewById(R.id.textView1);


    if (location==null)
    {
        txt.setText("-.- km/h");
    }
    else if (location == 1.50)
    {
        txt.setText("Warning");
    }
    else 
    {
        float nCurrentSpeed = location.getSpeed();
        txt.setText(nCurrentSpeed*3.6 + " km/h");
    }

}

I want to make it so that when the speed 1.50 km/h it changes the text to "Warning". I keep getting Incompatible operand types Location and double . I have tried doing this

    else if (nCurrentSpeed == 1.50)
{
    txt.setText("Warning");
}

But it still gives me the same error, but modified Incompatible operand types float and double . Is there any tips on how to solve this or how to create a speed limit for a speedometer?

The location object is not just a primitive object, but its contents is not know here.

However based upon later code, you are showing that it has

location.getSpeed()

so change your code to

else if (location.getSpeed() == 1.50)

I would also suggest that you use >= 1.5

shouldn't it just be something like

public void onLocationChanged(Location location) {
    TextView txt = (TextView) this.findViewById(R.id.textView1);


    if (location==null)
    {
        txt.setText("-.- km/h");
    }
    else if (location.getSpeed() >= 1.50f)
    {
        txt.setText("Warning");
    }
    else 
    {
        float nCurrentSpeed = location.getSpeed();
        txt.setText(nCurrentSpeed*3.6 + " km/h");
    }

}

ie you need to compare location.getSpeed() with 1.5, not the whole location Object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM