【发布时间】:2015-04-23 16:08:08
【问题描述】:
我正在尝试创建一个 if 语句,该语句通过“-1、0、1、2、3、4 和 5”检查餐厅的“评级”。然后我需要用图像替换评级数字。我已经尝试过这样做,但应用程序不断崩溃,应用程序在没有 if 语句的情况下工作正常,并显示 10 个最近的位置。这是代码:
JSONObject jo = (JSONObject) ja.get(i);
String name = jo.getString("BusinessName");
String rating = jo.getString("RatingValue");
String address1 = jo.getString("AddressLine1");
String address2 = jo.getString("AddressLine2");
String postcode = jo.getString("PostCode");
ImageSwitcher imageView = null;
if(rating.equalsIgnoreCase("-1")){
imageView.setImageResource(R.drawable.rating0);
}else if(rating.equalsIgnoreCase("0")){
imageView.setImageResource(R.drawable.rating0);
}else if(rating.equalsIgnoreCase("1")){
imageView.setImageResource(R.drawable.rating0);
}else{
//default case, if above all conditions will become false then this will call
}
【问题讨论】:
-
显然它会崩溃,因为
imageView为空。使用findViewById(R.id.viewid);初始化视图 -
看看 Android Studio 对您发布的代码的评价:方法调用 'imageView.setImageResource(R.drawable.rating0)' 可能会产生 'java.lang.NullPointerException'
标签: android imageswitcher