对于我所在地区的科学博览会,我正在为 Android 制作一个速度计应用程序,我想设置速度限制。这是我遇到问题的代码:
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");
}
}
我想让它在速度达到 1.50 km/h 时将文本更改为“警告”。我不断收到不兼容的操作数类型 Location 和 double
。我尝试过这样做
else if (nCurrentSpeed == 1.50)
{
txt.setText("Warning");
}
但它仍然给我同样的错误,但修改了不兼容的操作数类型float和double
。是否有任何关于如何解决此问题或如何为车速表创建速度限制的提示?
最佳答案
location
对象不仅仅是一个原始对象,但这里不知道它的内容。
但是根据后面的代码,您显示它具有
location.getSpeed()
所以将你的代码更改为
else if (location.getSpeed() == 1.50)
我还建议您使用>= 1.5
关于java - 如何为车速表 Android 应用程序设置速度限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482498/