Android 位置更改时显示或隐藏图像按钮

Android 位置更改时显示或隐藏图像按钮,android,image,camera,Android,Image,Camera,如何显示/隐藏图像按钮?如果我使用imagebutton.setVisiblity(View.go),应用程序就会崩溃。为什么? 顺便说一下,这是我的代码: if(acc > 0 && acc < 15){ b.setVisibility(View.VISIBLE); d.setVisibility(View.GONE); }else if(acc > 15){ b.setVisibility(View.GONE); d.setVisib

如何显示/隐藏图像按钮?如果我使用imagebutton.setVisiblity(View.go),应用程序就会崩溃。为什么?

顺便说一下,这是我的代码:

 if(acc > 0  && acc < 15){
  b.setVisibility(View.VISIBLE);
  d.setVisibility(View.GONE);

 }else if(acc > 15){
  b.setVisibility(View.GONE);
  d.setVisibility(View.VISIBLE);

}else{
  b.setVisibility(View.GONE);
  d.setVisibility(View.VISIBLE);
}
这是整个locationListener代码:在Location Manager下

@Override
public void onLocationChanged(Location location) {
f(location !=null){

double clat=location.getLatitude();
double clng=location.getLongitude();

DecimalFormat df = new DecimalFormat("###.######");
 vlatitude = df.format(clat);
vlongitude = df.format(clng);

int num1Lat = (int)Math.floor(clat);
int num2Lat = (int)Math.floor((clat - num1Lat) * 60);
double num3Lat = (clat - ((double)num1Lat+((double)num2Lat/60))) * 3600000;

int num1Lon = (int)Math.floor(clng);
int num2Lon = (int)Math.floor((clng - num1Lon) * 60);
double num3Lon = (clng - ((double)num1Lon+((double)num2Lon/60))) * 3600000;

flat = num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000";
flong = num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000";

if (clat > 0) {
latref = "N"; 
} else {
latref = "S";
}

if (clng> 0) {
longref = "E";    
} else {
longref = "W";
}
float f = location.getAccuracy();

int acc = Math.round(Float.valueOf(f));

if (acc !=0){
if(acc > 0  && acc < 15){
b.setVisibility(android.view.View.GONE);
d.setVisibility(android.view.View.VISIBLE);

}else if(acc > 15){
b.setVisibility(android.view.View.VISIBLE);
d.setVisibility(android.view.View.GONE);
}

}

}

}
@覆盖
已更改位置上的公共无效(位置){
f(位置!=null){
double clat=location.getLatitude();
double clng=location.getLongitude();
DecimalFormat df=新的DecimalFormat(“###########””;
vlatitude=df.format(clat);
Vlongite=df.格式(clng);
int num1Lat=(int)数学楼层(clat);
int num2Lat=(int)数学层((clat-num1Lat)*60);
双num3Lat=(clat-((双)num1Lat+((双)num2Lat/60))*3600000;
int num1Lon=(int)数学楼层(clng);
int num2Lon=(int)数学层((clng-num1Lon)*60);
双num3Lon=(clng-((双)num1Lon+((双)num2Lon/60))*3600000;
平面=num1Lat+“/1,“+num2Lat+”/1,“+num3Lat+”/1000”;
flong=num1Lon+“/1,“+num2Lon+”/1,“+num3Lon+”/1000”;
如果(clat>0){
latref=“N”;
}否则{
latref=“S”;
}
如果(clng>0){
longref=“E”;
}否则{
longref=“W”;
}
float f=location.getAccurance();
int acc=数学四舍五入(浮点值f);
如果(acc!=0){
如果(acc>0&&acc<15){
b、 setVisibility(android.view.view.GONE);
d、 setVisibility(android.view.view.VISIBLE);
}否则,如果(acc>15){
b、 setVisibility(android.view.view.VISIBLE);
d、 setVisibility(android.view.view.GONE);
}
}
}
}

可能是您的ImageButtons为空。可能您将侦听位置的方法放在ImageButton初始化的上方,您必须将其放在下方。是的,此按钮位于位置侦听器下方。这就是应用程序崩溃的原因吗?这在onLocationChanged下。@androidlearner什么是MainActivity.java第231行?
@Override
public void onLocationChanged(Location location) {
f(location !=null){

double clat=location.getLatitude();
double clng=location.getLongitude();

DecimalFormat df = new DecimalFormat("###.######");
 vlatitude = df.format(clat);
vlongitude = df.format(clng);

int num1Lat = (int)Math.floor(clat);
int num2Lat = (int)Math.floor((clat - num1Lat) * 60);
double num3Lat = (clat - ((double)num1Lat+((double)num2Lat/60))) * 3600000;

int num1Lon = (int)Math.floor(clng);
int num2Lon = (int)Math.floor((clng - num1Lon) * 60);
double num3Lon = (clng - ((double)num1Lon+((double)num2Lon/60))) * 3600000;

flat = num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000";
flong = num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000";

if (clat > 0) {
latref = "N"; 
} else {
latref = "S";
}

if (clng> 0) {
longref = "E";    
} else {
longref = "W";
}
float f = location.getAccuracy();

int acc = Math.round(Float.valueOf(f));

if (acc !=0){
if(acc > 0  && acc < 15){
b.setVisibility(android.view.View.GONE);
d.setVisibility(android.view.View.VISIBLE);

}else if(acc > 15){
b.setVisibility(android.view.View.VISIBLE);
d.setVisibility(android.view.View.GONE);
}

}

}

}