未报错,内容显示为null,无法正确显示获取的内容
WeatherActivity代码显示如下:
public class WeatherActivity extends AppCompatActivity {
public DrawerLayout drawerLayout;
private Button navButton;
public SwipeRefreshLayout swipeRefresh;
private ScrollView weatherLayout;
private TextView titleCity;
private TextView titleUpdateTime;
private TextView degreeText;
private TextView weatherInfoText;
private LinearLayout forecastLayout;
private TextView aqiText;
private TextView pm25Text;
private TextView comfortText;
private TextView carWashText;
private TextView sportText;
private String mWeatherId;
private ImageView bingPicImg;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
/**
* 处理并展示Weather实体类中的数据
*/
private void showWeatherInfo(Weather weather){
String cityName=weather.basic.cityName;
String updateTime=weather.basic.update.updateTime.split(" ")[1];
String degree=weather.now.temperature+"℃";
String weatherInfo=weather.now.more.info;
titleCity.setText(cityName);
titleUpdateTime.setText(updateTime);
degreeText.setText(degree);
weatherInfoText.setText(weatherInfo);
forecastLayout.removeAllViews();
for (Forecast forecast:weather.forecastList){
View view= LayoutInflater.from(this).inflate(R.layout.forecast_item,forecastLayout,false);
TextView dataText=view.findViewById(R.id.date_text);
TextView infoText=view.findViewById(R.id.info_text);
TextView maxText=view.findViewById(R.id.max_text);
TextView minText=view.findViewById(R.id.min_text);
dataText.setText(forecast.date);
infoText.setText(forecast.more.info);
maxText.setText(forecast.temperature.max);
minText.setText(forecast.temperature.min);
forecastLayout.addView(view);
}
if (weather.aqi!=null){
aqiText.setText(weather.aqi.city.aqi);
pm25Text.setText(weather.aqi.city.pm25);
}
String comfort="舒适度:"+weather.suggestion.comfort.info;
String carWash="洗车指数:"+weather.suggestion.carWash.info;
String sport="运动建议:"+weather.suggestion.sport.info;
comfortText.setText(comfort);
carWashText.setText(carWash);
sportText.setText(sport);
weatherLayout.setVisibility(View.VISIBLE);
Intent intent=new Intent(this, AutoUpdateService.class);
startService(intent);
}
}
xml布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?>
android:orientation="vertical"
android:layout_margin="15dp"
android:background="#8000"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="生活建议"
android:textColor="#fff"
android:textSize="20sp"/>
<TextView
android:id="@+id/comfort_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textColor="#fff"/>
<TextView
android:id="@+id/car_wash_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textColor="#fff"/>
<TextView
android:id="@+id/sport_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textColor="#fff"/>
GSON解析JSON代码如下:
public class Suggestion {
@SerializedName("comf")
public Comfort comfort;
@SerializedName("cw")
public CarWash carWash;
public Sport sport;
public class Comfort{
@SerializedName("text")
public String info;
}
public class CarWash{
@SerializedName("text")
public String info;
}
public class Sport{
@SerializedName("text")
public String info;
}
}
应该是得到的字符串“null”,而不是正的null,数据问题。
解决办法:
(1)
if("null".eques(forecast.date) || forecast.date == null) dataText.setText("");
else dataText.setText(forecast.date);
(2)数据实体别赋值“null”,后台数据控制或者数据实体中控制都可以。