简体   繁体   中英

Android PICASSO - Image Not Loading into ImageView + Stops all Statements following from occuring

Background: I have a Picasso Statement in my java file, which reads a JSON and then formats that data to the screen. The Issue: Once my JSON has been read, Picasso does not load Image from a URL to the ImageView, instead, it stops all statements after that from occurring, such as setting the text in a TextView

The JSON I am reading:

{  
   "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
}

I can retrieve the data and print that data to a TextView with no problems at all and all works fine. The code below is how I am retrieving the data:

                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");

So when formatting temp, description and city, I have no problems. NOTE: I am using a Fragment in my Activity and a file to fetch the data, which then formats the text view etc in the fragment as follows:

Tab1Fragment.txtCelcius.setText(temp);

The Issue arises when I use Picasso to try and obtain the "icon" value in he JSON, so "01n". I simply cannot get the image loading in and not only that, but all other processes then terminate?

For example:

Tab1Fragment.txtCelcius.setText(temp);  


Picasso.get().load("http://openweathermap.org/img/w/01d.png").into(Tab1Fragment.weatherIcon);
Tab1Fragment.txtCity.setText(city);

"temp" will be set to the text of txtCelcius, but Picasso will not load the URL and set the imageview AND the "name" statement will also not run, it will however, if I comment the Picasso line.

I use

String iconUrl = "http://openweathermap.org/img/w/"+icon+".png";

Then

 Picasso.get().load(iconUrl).into(Tab1Fragment.weatherIcon);

As I have read that is the best way of achieveing my task, but something isn't working and I can't see what exactly? My Picasso syntax I see is fine, and I do not see any errors in logcat etc..

All help is appreciated.

Edit: Tab1Fragment Code with ImageView declared

weatherIcon = (ImageView) rootView.findViewById(R.id.imageView);

Use this for old library :-

(implementation 'com.squareup.picasso:picasso:2.5.2')

Picasso.with(this).load(iconUrl).into(Tab1Fragment.weatherIcon);

instead of : For new library

(implementation 'com.squareup.picasso:picasso:2.71828')

Picasso.get().load(iconUrl).into(Tab1Fragment.weatherIcon);

I think you need to set load image from Picasso to ImageView inside Tab1Fragment .

And need to make sure Picasso load to ImageView after fragment already run onCreateView to inflate view and ImageView

I made an example

In activity

  PlaceholderFragment placeholderFragment = (PlaceholderFragment) mSectionsPagerAdapter.getItem(0);
  placeholderFragment.setIcon("http://openweathermap.org/img/w/01d.png");

Fragment

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM