背景:我的 java 文件中有一个 Picasso 语句,它读取 JSON,然后将该数据格式化到屏幕上。问题:读取 JSON 后,Picasso 不会将图像从 URL 加载到 ImageView,而是停止此后发生的所有语句,例如在 TextView 中设置文本
我正在阅读的 JSON:
{
"coord":{
"lon":139.01,
"lat":35.02
},
"weather":[
{
"id":800,
"main":"Clear",
"description":"clear sky",
"icon":"01n"
}
],
"base":"stations",
"main":{
"temp":285.514,
"pressure":1013.75,
"humidity":100,
"temp_min":285.514,
"temp_max":285.514,
"sea_level":1023.22,
"grnd_level":1013.75
},
"wind":{
"speed":5.52,
"deg":311
},
"clouds":{
"all":0
},
"dt":1485792967,
"sys":{
"message":0.0025,
"country":"JP",
"sunrise":1485726240,
"sunset":1485763863
},
"id":1907296,
"name":"Tawarano",
"cod":200
}
我可以检索数据并将该数据打印到 TextView,没有任何问题,并且一切正常。下面的代码是我检索数据的方式:
JSONObject jo = new JSONObject(data);
JSONObject main_object = jo.getJSONObject("main");
JSONArray array = jo.getJSONArray("weather");
JSONObject object = array.getJSONObject(0);
String icon = object.getString("icon");
String temp = String.valueOf(main_object.getDouble("temp"));
String description = object.getString("description");
String city = jo.getString("name");
因此,在格式化 temp、description 和 city 时,我没有任何问题。注意:我在活动中使用片段和文件来获取数据,然后格式化片段中的文本视图等,如下所示:
Tab1Fragment.txtCelcius.setText(temp);
当我使用 Picasso 尝试获取 JSON 中的“icon”值(即“01n”)时,就会出现问题。我根本无法加载图像,不仅如此,所有其他进程都会终止?
例如:
Tab1Fragment.txtCelcius.setText(temp);
Picasso.get().load("http://openweathermap.org/img/w/01d.png").into(Tab1Fragment.weatherIcon);
Tab1Fragment.txtCity.setText(city);
“temp”将设置为txtCelcius的文本,但Picasso不会加载URL并设置imageview,并且“name”语句也不会运行,但是,如果我评论Picasso行,它会运行。
I use
String iconUrl = "http://openweathermap.org/img/w/"+icon+".png";
Then
Picasso.get().load(iconUrl).into(Tab1Fragment.weatherIcon);
正如我所读到的,这是完成我的任务的最佳方式,但有些东西不起作用,我看不出到底是什么?我看到的 Picasso 语法很好,并且在 logcat 等中没有看到任何错误。
感谢所有帮助。
编辑:声明了 ImageView 的 Tab1Fragment 代码
weatherIcon = (ImageView) rootView.findViewById(R.id.imageView);