【问题标题】:Android PICASSO - Image Not Loading into ImageView + Stops all Statements following from occuringAndroid PICASSO - 图像未加载到 ImageView + 停止所有语句的发生
【发布时间】: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 网站上的最新版本)
  • 您是否收到任何日志或应用程序崩溃?

标签: java android json picasso


【解决方案1】:

将此用于旧库:-

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

【讨论】:

【解决方案2】:

我认为您需要从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);
    }
}

【讨论】:

    猜你喜欢
    • 2019-06-27
    • 2023-03-28
    • 2016-02-08
    • 2018-07-14
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    相关资源
    最近更新 更多