我有错误行
FragmentManager fm = getFragmentManager();
点击时传递活动的任何解决方案?那么我需要什么?
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(final RecyclerViewHolders holder, final int position) {
holder.cardText.setText(itemList.get(position).getName());
holder.cardPhoto.setImageResource(itemList.get(position).getPhoto());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
switch (position){
case 0:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frameLayout, new FragmentNewGame(), "newGame");
ft.addToBackStack(null);
ft.commit();
答案 0 :(得分:3)
替换它:
到此:
FragmentManager fm = getFragmentManager();
然后您的代码应如下所示:
FragmentManager fm = ((Activity)context).getFragmentManager();
或者
FragmentManager fm = ((Activity)context).getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frameLayout, new FragmentNewGame(), "newGame");
ft.addToBackStack(null);
ft.commit();