使用 glide 在 recyclerView 中加载图像时出错

error while loading images in recyclerView using glide

我有一个名为 "RecyclerTab1" 的片段,它包含 recyclerview 布局。"HomeManager" 是 recyclerview adapter.Within 我使用 Glide 从 drawable 加载图像 folder.Whe我尝试 运行 我收到错误 java.lang.IllegalArgumentException: 的程序。我还包括 RecyclerTab1 class 和 Recyclerview

的 HomeManager 适配器

RecyclerTab1:

package com.example.root5solutions.mirrealtors.hometabfragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.root5solutions.mirrealtors.HomeRecyclerDetailActivity;
import com.example.root5solutions.mirrealtors.R;
import com.example.root5solutions.mirrealtors.projectdatabase.HomeManager;
import com.example.root5solutions.mirrealtors.projectdatabase.HomeData;
import com.example.root5solutions.mirrealtors.projectdatabase.RecyclerClickListener;
import com.kogitune.activity_transition.ActivityTransitionLauncher;

public class RecyclerTab1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.home_tab1_recycler, container, false);
        RecyclerView rv = (RecyclerView) v.findViewById(R.id.home_recyclerview);
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        rv.setLayoutManager(llm);
        rv.smoothScrollToPosition(View.SCROLLBAR_POSITION_DEFAULT);
        rv.setHasFixedSize(true); // to improve performance
        rv.setAdapter(new HomeManager(getContext())); // the projectdatabase manager is assigner to the RV

        rv.addOnItemTouchListener( // and the click is handled
                new RecyclerClickListener(getContext(), new RecyclerClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(getActivity(), HomeRecyclerDetailActivity.class);
                        intent.putExtra(HomeRecyclerDetailActivity.ID, HomeData.PROJECTS[position].getId());
                        ActivityTransitionLauncher.with(getActivity()).from(view).launch(intent);
                    }
                }));
        return v;
    }
}

家庭经理:

package com.example.root5solutions.mirrealtors.projectdatabase;

import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.root5solutions.mirrealtors.R;


public class HomeManager extends RecyclerView.Adapter<HomeManager.RecyclerViewHolder> {
    private Context context;
public HomeManager(Context context){
    this.context=context;
}
    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {

        TextView mProjectName, mProjectCity, mProjectType;
        ImageView mImage;
        CheckBox mCheck;
        RelativeLayout mProjectStatus;


        RecyclerViewHolder(View itemView) {
            super(itemView);
            mProjectName = (TextView) itemView.findViewById(R.id.PROJECT_name);
            mProjectCity = (TextView) itemView.findViewById(R.id.PROJECT_city);
            mProjectType = (TextView) itemView.findViewById(R.id.PROJECT_type);
            mImage = (ImageView) itemView.findViewById(R.id.PROJECT_image);
            mCheck = (CheckBox) itemView.findViewById(R.id.PROJECT_fav);
            mProjectStatus = (RelativeLayout) itemView.findViewById(R.id.label);
        }
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_item, viewGroup, false);
        return new RecyclerViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder viewHolder, int i) {
        // get the single element from the main array

        final HomeData projects = HomeData.PROJECTS[i];
        // Set the values
        viewHolder.mProjectName.setText(projects.get(HomeData.Field.NAME));
        viewHolder.mProjectCity.setText(projects.get(HomeData.Field.CITY));
        viewHolder.mProjectType.setText(projects.get(HomeData.Field.TYPE));
        viewHolder.mImage.setImageResource(projects.geti(HomeData.Field.IMAGE));
        viewHolder.mCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(v, "Item Favorited", Snackbar.LENGTH_SHORT);
                snackbar.show();
            }
        });

        Glide.with(context)
                .load(Gallery_Constants.IMAGES)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(viewHolder.mImage);
        if (projects.get(HomeData.Field.STATUS) == "FOR SALE") {
            viewHolder.mProjectStatus.setBackgroundResource(R.drawable.labelsale);
        } else if (projects.get(HomeData.Field.STATUS) == "SOLD OUT") {
            viewHolder.mProjectStatus.setBackgroundResource(R.drawable.labelsold);
        }

    }


    @Override
    public int getItemCount() {

        return HomeData.PROJECTS.length;
    }


}

Logcat:

12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime: FATAL EXCEPTION: main
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime: Process: com.example.root5solutions.mirrealtors, PID: 8509
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime: java.lang.IllegalArgumentException: Unknown type class [Ljava.lang.String;. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at com.bumptech.glide.RequestManager.load(RequestManager.java:598)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at com.example.root5solutions.mirrealtors.projectdatabase.HomeManager.onBindViewHolder(HomeManager.java:68)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at com.example.root5solutions.mirrealtors.projectdatabase.HomeManager.onBindViewHolder(HomeManager.java:19)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5212)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5245)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4483)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1133)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.View.layout(View.java:15686)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.view.ViewGroup.layout(ViewGroup.java:5107)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:     at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
12-01 12:30:17.368 8509-8509/com.example.root5solutions.mirrealtors E/AndroidRuntime:   at android.view.View.layout(View

Glide 提供这么多 .with() 方法是有原因的:它遵循生命周期。

想象一下动态添加到 Activity 的片段。在它的 onCreateView 方法中,它启动一个 3MB 图像的 Glide 加载。现在,如果用户按下后退按钮并且 Fragment 被删除或整个 activity 关闭怎么办?

  1. 如果你使用 with(getActivity().getApplicationContext()) 什么都不会 发生了,所有 3MB 的数据都被下载,然后被解码、缓存, 甚至可能设置为 ImageView,然后对其进行垃圾回收, 因为对它的唯一引用来自 Glide 内部。

  2. 如果你用with((Fragment)this) Glide订阅了Fragment的 生命周期事件,一旦片段停止,任何 应暂停未完成的请求;当被摧毁时,所有 待处理的请求被清除。这意味着图像下载将 中途停止,死者将不再使用资源 片段.

  3. 如果你使用with(getActivity()) Glide订阅了Activity的 生命周期事件和上面发生的同样的事情,但只有当 Activity 被停止或销毁。所以最好的做法是使用 尽可能接近 context/fragment 以避免未使用的请求 完成。

希望对你有帮助..

你得到这样的错误:

IllegalArgumentException: Unknown type class [Ljava.lang.String;

它可能为 Glide.load() 提供了一个 String[]。

请在此处查看:

Glide.with(context)
            .load(Gallery_Constants.IMAGES)
            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .into(viewHolder.mImage);

确保 Gallery_Constants.IMAGES 的类型正确。