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

后端 未结 2 586
难免孤独
难免孤独 2021-01-27 11:35

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 re

相关标签:
2条回答
  • 2021-01-27 11:52

    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);
    
    0 讨论(0)
  • 2021-01-27 11:57

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题