【发布时间】:2019-03-08 07:05:54
【问题描述】:
背景: 我的 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 中的“图标”值时出现问题,即“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 行,它将不会运行。
我用
String iconUrl = "http://openweathermap.org/img/w/"+icon+".png";
然后
Picasso.get().load(iconUrl).into(Tab1Fragment.weatherIcon);
正如我所读到的,这是完成我的任务的最佳方式,但有些东西不起作用,我看不出究竟是什么?我看到的毕加索语法很好,我在 logcat 等中没有看到任何错误。
感谢所有帮助。
编辑:声明了 ImageView 的 Tab1Fragment 代码
weatherIcon = (ImageView) rootView.findViewById(R.id.imageView);
【问题讨论】:
-
是否在 Tab1Fragment 之外设置了
Tab1Fragment图标?您能否添加更多有问题的 Tab1Fragment 代码? -
请查看更新后的问题。
-
请在 gradle 中检查您的毕加索版本。如果您可以发布 gradle 有 Picasso 部分来检查版本,那就更好了。因为毕加索 v1 和 v2 有区别
-
实现 'com.squareup.picasso:picasso:2.71828'(Picasso 网站上的最新版本)
-
您是否收到任何日志或应用程序崩溃?