我之前看了这个问题的答案,但我不明白如何解决我的问题。
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AkisFragment NAME = new AkisFragment();
fragmentTransaction.replace(R.id.fragment_tutucu, NAME);
fragmentTransaction.commit();
注意:我的类必须是静态的.getFragmentManager()方法在静态类中是如何工作的?getFragmentManager()方法是Fragment类的一个方法。
FragmentManager类的对象是通过getFragmentManager()来创建和同步的.我不明白为什么不希望静态类的getFragmentManager()方法工作。
已发出无关代码。
我的java代码。
public static class KelimelerAdapterx extends RecyclerView.Adapter { / boolean f; private Context mContext; public List kelimelerListe; public ArrayList teststringsx = new ArrayList<>()。
String svvs; Fragment fragment; private int mScrollState = AbsListView.OnScrollListener.SCROLL_STATE_IDLE; public RecyclerView rv; testc fd = new testc(); public KelimelerAdapterx(Context mContext, List<Kelimeler> kelimelerListe) { this.mContext = mContext; this.kelimelerListe = kelimelerListe; } @Override public KelimelerAdapterx.CardTasarimTutucu onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_tasarim,parent,false); // Log.e("scrolldurumburcussv", String.valueOf(scrolldurum)); return new KelimelerAdapterx.CardTasarimTutucu(view); } @Override public void onBindViewHolder(final KelimelerAdapterx.CardTasarimTutucu holder, int position) { final Kelimeler kelime = kelimelerListe.get(position); teststring fe = new teststring(position); Log.e("armutlaaarrsss", String.valueOf(fe.getId())); holder.textViewIngilizce.setText(String.valueOf(kelime.getId())); holder.textViewTurkce.setText(kelime.getLikebtn()); holder.ad.setText(kelime.getAd()); holder.ad.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("t4rryert", "dddddgsg"); Profil profil = new Profil(); profil.idvalue(kelime.getId()); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); AkisFragment NAME = new AkisFragment(); fragmentTransaction.replace(R.id.fragment_tutucu, NAME); fragmentTransaction.commit(); } }); testc f = new testc(); Log.e("armutelma", String.valueOf(f.elma)); } @Override public int getItemCount() { return kelimelerListe.size(); } public static class CardTasarimTutucu extends RecyclerView.ViewHolder{ public TextView textViewIngilizce; public TextView textViewTurkce; public CardView kelime_card; SimpleVideoView videoView; RecyclerView.RecyclerListener recyclerListener; View view; ScrollView scrollView; private RecyclerView rvx; boolean fddd; Button btn,btntwo,btnthree,start_button,ad; AkisFragment fff ; boolean muz = true; private boolean playWhenReady = true; private int currentWindow = 0; private long playbackPosition = 0; int i = 0; public CardTasarimTutucu(View itemView) { super(itemView); textViewIngilizce = itemView.findViewById(R.id.textViewIngilizce); textViewTurkce = itemView.findViewById(R.id.textViewTurkce); ad = itemView.findViewById(R.id.ad); kelime_card = itemView.findViewById(R.id.kelime_card); videoView = itemView.findViewById(R.id.videoview); scrollView = itemView.findViewById(R.id.scrollView); btn = itemView.findViewById(R.id.btn); btntwo = itemView.findViewById(R.id.btntwo); btnthree = itemView.findViewById(R.id.btnthree); } public boolean veripost(boolean one){ Log.e("renkssss", String.valueOf(one)); Log.e("renkf", String.valueOf(fddd)); return one; } } }
下面的代码在静态类中无法使用。
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AkisFragment NAME = new AkisFragment();
fragmentTransaction.replace(R.id.fragment_tutucu, NAME);
fragmentTransaction.commit();
当我不再把我的类做成静态的时候,我就解决了这个问题。