简体   繁体   English

什么上下文名称应该传递给Glide方法?

[英]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. 为了在使用viewholderfragment时加载图像,我不知道应该传递上下文对象名称。 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. 您正在创建一个新的Activity,而不是获取当前的Activity,这就是您收到该错误的原因。 You should do: 你应该做:

 Glide.with(getActivity())...

Ideally, you would pass the Fragment context into your RecyclerView , and use that context in the call. 理想情况下,您将Fragment上下文传递到RecyclerView ,并在调用中使用该上下文。 The documents say: 文件说:

public static RequestManager with(Context context) 公共静态RequestManager with(Context context)

Begin a load with Glide by passing in a context. 通过传递上下文开始使用Glide进行加载。 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. 通常,应从使用结果的级别开始加载。如果在子片段的视图中使用资源,则应使用该子片段以(android.app.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. 同样,如果在父片段的视图中使用该资源,则应使用父片段以(android.app.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)}. 同样,如果将资源用于活动的视图中,则应以(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. 参数:context-任何上下文,将不会保留。 Returns: A RequestManager for the top level application that can be used to start a load. 返回:用于顶层应用程序的RequestManager,可用于启动加载。

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 . 然后,您可以使用activity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递给下面给出的方法的 prev_node 参数的名称应该是什么? - What should be the name of the prev_node argument passed into the method given below? 在pickTrees中返回treeHelper方法时应该传递什么作为“树”? - What should be passed as "tree" when returning the treeHelper method in pickTrees? 什么是传递给BroadcastReceiver的onReceive()的Context? - What is the Context passed into onReceive() of a BroadcastReceiver? 什么参数应该传递给.show()? - What arguments should be passed to .show()? 应该将什么作为源传递给构造函数? - What should be passed to the constructor as the source? 我应该在 Glide.with() function 中使用什么视图 - What's the view I should use in Glide.with() function HTTPS主机名错误:应为 <xxx.xxx.xxx.xxx> ,请检查URL欺骗。 传递的主机名是什么? - HTTPS hostname wrong: should be <xxx.xxx.xxx.xxx>, checkURLSpoofing. What is the host name being passed? 应该使用什么名称的方法来返回嵌套对象的数量? - What name should have method which returns number of nested objects? 传递给getAssets()方法的上下文为null - Context being passed into method for getAssets() is null Android:传递给方法时上下文为空 - Android: Context is null when passed to method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM