简体   繁体   中英

What context name should be passed to Glide method?

In order to load image when using viewholder and fragment , I don't know context object name I should pass. Below is my glide code:

Glide.with(activity).load(cheeses.getImageView()).fitCenter().into(mImageView);

Error:

at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1555) 
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696)
at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:667)
at com.bumptech.glide.manager.RequestManagerRetriever.getSupportRequestManagerFragment(RequestManagerRetriever.java:187)
at com.bumptech.glide.manager.RequestManagerRetriever.supportFragmentGet(RequestManagerRetriever.java:195)
at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:104)
at com.bumptech.glide.Glide.with(Glide.java:644)
at com.support.android.designlibdemo.ViewHolderClass.ViewHolder.bindToCheese(ViewHolder.java:42)
at com.support.android.designlibdemo.CheeseListFragment$1.populateViewHolder(CheeseListFragment.java:112)
at com.support.android.designlibdemo.CheeseListFragment$1.populateViewHolder(CheeseListFragment.java:93)

在ViewHolder中使用getActivity,而不是MainActivity mainActvity;

The issue is here:

 Glide.with(activity)

You are creating a new Activity, rather than getting the current activity, which is why you get that error. You should do:

 Glide.with(getActivity())...

Ideally, you would pass the Fragment context into your RecyclerView , and use that context in the call. The documents say:

public static RequestManager with(Context context)

Begin a load with Glide by passing in a context. Any requests started using a context will only have the application level options applied and will not be started or stopped based on lifecycle events. In general, loads should be started at the level the result will be used in. If the resource will be used in a view in a child fragment, the load should be started with with(android.app.Fragment)} using that child fragment. Similarly, if the resource will be used in a view in the parent fragment, the load should be started with with(android.app.Fragment) using the parent fragment. In the same vein, if the resource will be used in a view in an activity, the load should be started with with(android.app.Activity)}.

This method is appropriate for resources that will be used outside of the normal fragment or activity lifecycle (For example in services, or for notification thumbnails).

Parameters: context - Any context, will not be retained. Returns: A RequestManager for the top level application that can be used to start a load.

Source

You could do:

public ViewHolder(View view, Context context) {
    super(view);
    mView = view;
    mImageView = (ImageView) view.findViewById(R.id.avatar);
    mTextView = (TextView) view.findViewById(android.R.id.text1);
    activity = context;
}

Then you could use activity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM