picasso错误android.widget.RelativeLayout无法强制转换为android.widget.ImageView

时间:2015-09-04 19:32:51

标签: android picasso

我理解它是因为我的list_item.xml但原因是什么

我收到此错误

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.ImageView
E/AndroidRuntime(27089):    at com.example.jsn.ImageListAdapter.getView(ImageListAdapter.java:44)

这是我的代码

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }

        Picasso
            .with(context)
            .load(imageUrls[position])
            .placeholder(R.drawable.ic_launcher) // can also be a drawable
            .fit() // will explain later
            .noFade()
            .into((ImageView) convertView);  
        return convertView; // I guess here is my problem

    } 
}

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0">

<ImageView 
    android:id="@+id/ImageView"
       android:layout_width="match_parent"
       android:layout_height="200dp"/>

</RelativeLayout>

当我的list_item.xml像这样:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ImageView"
       android:layout_width="match_parent"
       android:layout_height="200dp"/>

但为什么呢?如何使其与relativelayout一起使用

1 个答案:

答案 0 :(得分:3)

您的inflater.inflate(R.layout.list_item, parent, false);list_item.xml的结果,这意味着它是RelativeLayout的根元素的实例,即 Picasso .with(context) .load(imageUrls[position]) .placeholder(R.drawable.ic_launcher) // can also be a drawable .fit() // will explain later .noFade() .into((ImageView) convertView.findViewbyId(R.id.ImageView));

要解决此问题,您需要执行以下操作:

TMSApp.filter('newlinesmatch', function () {
    return function(text) {
        var str = '<br>';
        var count = (text.match(/<br>/g) || []).length;
        if(count > 2){
            return String(text).replace(/<br>/g,'<br><br>');    
        }
    }
});