java - 应将什么上下文名称传递给 Glide 方法?

标签 java android firebase firebase-realtime-database android-glide

为了在使用 viewholderfragment 时加载图像,我不知道应该传递的上下文对象名称。下面是我的滑动代码:

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

错误:

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)

最佳答案

问题在这里:

 Glide.with(activity)

您正在创建一个新的 Activity ,而不是获取当前的 Activity ,这就是您收到该错误的原因。你应该这样做:

 Glide.with(getActivity())...

理想情况下,您可以将 Fragment 上下文传递到 RecyclerView 中,并在调用中使用该上下文。文件说:

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

你可以这样做:

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

然后您可以使用activity

关于java - 应将什么上下文名称传递给 Glide 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43162717/

相关文章:

java - 在 TimeoutMainSubscriber 的情况下将 Reactor Context 复制到 MDC

java.rmi.ConnectIOException : non-JRMP server at remote endpoint

java - 如何使用sqlite在java中创建条件

javascript - 无法删除 firebase 中的元素和子元素

javascript - 尝试使用 id 访问数据时,firebase 规则中的通配符不起作用

java - 我想获取特定月份的周数

Android APK 在 google play 中不可见

android - 当数据库中的数据发生变化时得到通知

android - 想要发布第一个 App 我需要知道什么

java - 当前用户的DataSnapshot值为NULL