卡住了一点点.我尝试将getActivity更改为’c'(上下文),但getSupportFragmentManager获得相同的错误.阅读不同的主题,但找不到解决方案.检查过库并尝试更改它们但没有.非常感谢一些帮助!
import androID.app.Activity; import androID.content.Context; import androID.support.v4.app.FragmentManager; import androID.support.v4.app.FragmentTransaction; import androID.support.v4.app.DialogFragment; import androID.support.v4.app.Fragment; import androID.support.v7.Widget.RecyclerVIEw; import androID.vIEw.LayoutInflater; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup; import androID.Widget.Toast; import com.bumptech.glIDe.GlIDe; import com.bumptech.glIDe.load.engine.diskCacheStrategy; import com.example.mrti.menu.Fragments.MealsListFragment; import com.example.mrti.menu.R; import java.util.ArrayList; public class categoryAdapter extends RecyclerVIEw.Adapter<VIEwHolder> {Context c;ArrayList<category> CategorIEs;public categoryAdapter(Context c, ArrayList<category> CategorIEs) { this.c = c; this.CategorIEs = CategorIEs;}// INITIAliZE HolDER@OverrIDepublic VIEwHolder onCreateVIEwHolder(VIEwGroup vIEwGroup, int vIEwType) { VIEw v = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.rec_item, null); VIEwHolder holder = new VIEwHolder(v); return holder;}@OverrIDepublic voID onBindVIEwHolder(VIEwHolder holder, final int position) { // BIND DATA TO VIEWS holder.mnameTextVIEw.setText(CategorIEs.get(position).getname()); // LOAD IMAGES FROM WEB AND CACHE THEM GlIDe.with(c) .load(CategorIEs.get(position).getimage()) .diskCacheStrategy(diskCacheStrategy.ALL) // Saves the media item after all transformations to cache and // Saves just the original data to cache. .placeholder(R.mipmap.ic_launcher) .into(holder.img); // ListENER holder.setItemClickListener(new itemClickListener() { @OverrIDe public voID onItemClick(VIEw v, int pos) { Toast.makeText(c, CategorIEs.get(pos).getname(), Toast.LENGTH_SHORT ).show(); MealsListFragment fragment = new MealsListFragment(); FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.ID.root_layout, fragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); }
解决方法:
Cannot resolve getActivity() in fragment
您的java类声明如下:
public class categoryAdapter extends RecyclerVIEw.Adapter<VIEwHolder>
categoryAdapter不是片段. categoryAdapter是RecyclerVIEw.Adapter.
RecyclerView.Adapter
没有getActivity()方法.因此,您的代码将无法编译.
您似乎想要在RecyclerVIEw.Adapter中执行FragmentTransaction.但是,为了能够调用getSupportFragmentManager(),您需要托管与categoryAdapter相连的RecyclerVIEw的FragmentActivity.
但是,RecyclerVIEw.Adapter没有对FragmentActivity的直接或间接访问权限.因此,您需要将FragmentActivity放入categoryAdapter.
您当前的构造函数声明为:
public categoryAdapter(Context c, ArrayList<category> CategorIEs)
我的猜测是,在你的活动中,你正在调用新的categoryAdapter(this,somelistofcategorIEs).如果是这样,请将构造函数更改为:
public categoryAdapter(FragmentActivity c, ArrayList<category> CategorIEs)
并将您的字段更改为FragmentActivity.然后,您可以更改:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
至:
FragmentManager fragmentManager = c.getSupportFragmentManager();
总结 以上是内存溢出为你收集整理的android – 无法在recyclerView.adapter中解析getActivity()全部内容,希望文章能够帮你解决android – 无法在recyclerView.adapter中解析getActivity()所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)