我试图设置一个可点击的cardview,这样当点击列表中的卡片时,片段会在我的recyclerview中展开。到目前为止,我可以点击注册并显示位置,如果我敬酒,但当我尝试在fragment_container中膨胀新视图时,我一直收到错误。
下面显示的代码抛出“错误:(49,54)错误:非静态方法getFragmentManager()无法从静态上下文引用”
有人可以告诉我如何解决此错误吗?
package com.packagename.adapters_holders;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.packagename.R;
import com.packagename.WhatIWantToDisplayFragment;
/**
* RecyclerView Holder
*/
public abstract class MyPageHolder extends RecyclerView.ViewHolder{
protected Activity activity;
protected MyPageAdapter adapter;
public TextView txtTitle, txtDescription;
public ImageView imgImage;
public View view;
public MyPageHolder(final View v, final Activity activity, MyPageAdapter adapter) {
super(v);
this.activity = activity;
this.adapter = adapter;
imgImage = (ImageView) v.findViewById(R.id.cardview_image);
txtTitle = (TextView) v.findViewById(R.id.cardview_title);
txtDescription = (TextView) v.findViewById(R.id.cardview_description);
view = (CardView) v.findViewById(R.id.card_view);
view = itemView;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//what to put here
FragmentManager fm = FragmentActivity.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new WhatIWantToDisplayFragment(), "fragment_screen");
ft.commit();
break;
}
});
}
public void bind(ListItems dataModel, Activity activity, int position) {
ListItems m = (ListItems)dataModel;
imgImage.setImageResource(m.image);
txtTitle.setText(m.title);
txtDescription.setText(m.description);
}
}
答案 0 :(得分:0)
您不能像那样创建FragmentManager
个实例
FragmentManager fm = FragmentActivity.getFragmentManager();
,
而是使用活动实例来创建一个
FragmentManager fm = activity.getFragmentManager();