背景: 我的java文件中有一个毕加索语句,该语句读取JSON,然后将数据格式化到屏幕上。 问题:一旦读取了我的JSON,毕加索就不会将图像从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");
因此,在格式化温度,描述和城市时,我没有任何问题。注意:我在“活动”中使用一个片段,并使用一个文件来获取数据,然后按如下所示格式化片段中的文本视图等:
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”语句也不会运行,但是,如果我注释毕加索行,则会这样。
我用
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);
答案 0 :(得分:2)
将其用于旧库:-
(implementation 'com.squareup.picasso:picasso:2.5.2')
Picasso.with(this).load(iconUrl).into(Tab1Fragment.weatherIcon);
代替: 对于新库
(implementation 'com.squareup.picasso:picasso:2.71828')
Picasso.get().load(iconUrl).into(Tab1Fragment.weatherIcon);
答案 1 :(得分:0)
我认为您需要从Picasso to ImageView inside Tab1Fragment
设置加载图片。
并且需要确保在片段已经运行 Picasso load to ImageView
之后onCreateView
才能填充视图和ImageView
我做了一个例子
活动中
PlaceholderFragment placeholderFragment = (PlaceholderFragment) mSectionsPagerAdapter.getItem(0);
placeholderFragment.setIcon("http://openweathermap.org/img/w/01d.png");
片段
public class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
private ImageView imageView;
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
this.imageView = rootView.findViewById(R.id.imageView);
return rootView;
}
public void setIcon(String url) {
if (imageView == null) {
throw new RuntimeException("ImageView null, please make sure this setIcon function run after onCreateView");
}
Picasso.get().load(url).into(imageView);
}
}