Java Android Toast在布尔声明后不显示
上面写着Java Android Toast在布尔声明后不显示,java,android,toast,Java,Android,Toast,上面写着不可访问的祝酒词没有出现,但是上面所有的祝酒词都出现了,为什么我的代码会在那一行停止执行 if (msg_from.equals(MainActivity.PHONENUMBER)){ if (msgBody.startsWith("Your")){ //have seen this toast Toast.makeText(context, "Yes!
不可访问
的祝酒词没有出现,但是上面所有的祝酒词都出现了,为什么我的代码会在那一行停止执行
if (msg_from.equals(MainActivity.PHONENUMBER)){
if (msgBody.startsWith("Your")){
//have seen this toast
Toast.makeText(context, "Yes!!", Toast.LENGTH_SHORT).show();
//have seen this toast
LocationManager imLoca = SysService.locationMan;
Toast.makeText(context, "Yes 2!!", Toast.LENGTH_SHORT).show();
//have seen
boolean gpsEnabled = imLoca.isProviderEnabled(LocationManager.GPS_PROVIDER);
Toast.makeText(context, "not reachable", Toast.LENGTH_SHORT).show();
//but this does not show up, and any thing after this line is not working;
boolean netOn = imLoca.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gpsEnabled){
//do some thing
}
else if (netOn){
//do some thing
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
}
}
Imloca返回null。问题是 我将此代码复制到我的测试项目中,并为gpsEnabled和netOn的if和else if添加了祝酒词 看起来第一个if是在gpsEnabled为true时执行的if 希望这有帮助,祝你好运
if (gpsEnabled){
//do some thing
Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
}
else if (netOn){
//do some thing
Toast.makeText(context, "2", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "Failing", Toast.LENGTH_SHORT).show();
}
您的应用程序可能因为
imLoca
为空或其他原因而崩溃。你的代码有权调用GPS吗?您可能会遇到一个异常,即callinstaed显示toast使用Log.i()方法获取代码运行信息Log您的应用程序崩溃了吗?(例如NullPointerException)@dube权限调用正确,我有一个类SysService
,工作正常问题是imLoca
为null问题是imLoca
为null
,这是一个很好的例子,说明了为什么总是使用花括号。