使用glide

时间:2015-12-01 07:11:03

标签: android recycler-adapter android-glide

我有一个名为&#34; RecyclerTab1&#34;它保存了recyclerview布局。&#34; HomeManager&#34;是recyclelerview适配器。在Recyclerview适配器中我使用Glide从drawable文件夹加载图像。我尝试运行程序时收到错误java.lang.IllegalArgumentException:。我还包括RecyclerTab1类和HomeManager适配器for recyclerview < / p>

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

HomeManager:

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

2 个答案:

答案 0 :(得分:1)

你得到的错误是这样的:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

int worker_running;
pthread_t th_worker;

int worker2_running;
pthread_t th_worker2;

void *
worker2(void *data)
{

    // tell main we're fully functional
    worker2_running = 1;

    for (int i = 0; i < 1000000; i++) {
        printf("thread for worker2----%d\n", i);
        usleep(500);
    }

    return NULL;
}

void *
worker(void *data)
{

    // tell main we're fully functional
    worker_running = 1;

    pthread_create(&th_worker2, NULL, worker2, data);

    for (int i = 0; i < 100; i++) {
        printf("thread for worker-----%d\n", i);
        usleep(500);
    }

    return NULL;
}

void
join(pthread_t _th)
{
    pthread_join(_th, NULL);
}

int
main()
{
    char *str = "hello thread";

    pthread_create(&th_worker, NULL, worker, (void *) str);

    // give worker enough time to properly start worker2
    // NOTE: this not necessarily needed as loop below is better
    while (! th_worker2)
        usleep(100);

    // give worker2 enough time to completely start
    while (! worker2_running)
        usleep(100);

    /* problem in here (not anymore!) */
    join(th_worker2);

    return 1;
}

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

请点击这里:

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

确保Gallery_Constants.IMAGES的类型正确无误。

答案 1 :(得分:0)

Glide提供了许多.with()方法,原因如下:它遵循生命周期。

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

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

  2. 如果你使用((片段)这个)Glide订阅片段 生命周期事件,一旦片段停止,任何 暂停要求应暂停;所有人都被摧毁了 等待请求被清除。这意味着图像下载将 中途停止,死者不再使用资源 片段。

  3. 如果你使用(getActivity())Glide订阅了Activity 生命周期事件和上面发生的事情相同,但仅限于 活动被停止或销毁。所以最好的做法是使用 最近的可能上下文/片段以避免未使用的请求 完井。

  4. 希望这可以帮助你..