晚上好。我在stackoverflow上阅读了几个问题,并在片段中找到了一些教程文章和站点。我当前正在将有关第一个片段类的一些数据传递给第二个片段类。但是我在捆绑中的事务处理方面有一些错误,它指出错误中的方法getSupportFragmentManager()方法未定义。

 getSupportFragmentManager().beginTransaction()
         .replace(R.id.content_frame, cblf).commit();


解决此问题的最佳方法是什么?
无论如何,这是我的整个代码,

package com.example.navigationdrawerexample;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CollegeBulletinFragment extends Fragment implements OnClickListener {

    Button ccs;
    Button coe;
    Button coed;
    Button con;
    Button cba;
    Button cas;
    Button cihm;
    Button gn;
    public CollegeBulletinFragment(){
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_collegebulletin, container, false);

        gn = (Button) rootView.findViewById(R.id.gn);
        ccs = (Button) rootView.findViewById(R.id.ccs);
        coe = (Button) rootView.findViewById(R.id.coe);
        coed = (Button) rootView.findViewById(R.id.coed);
        con = (Button) rootView.findViewById(R.id.con);
        cba = (Button) rootView.findViewById(R.id.cba);
        cas = (Button) rootView.findViewById(R.id.cas);
        cihm = (Button) rootView.findViewById(R.id.cihm);

        gn.setOnClickListener(this);
        ccs.setOnClickListener(this);
        coe.setOnClickListener(this);
        coed.setOnClickListener(this);
        con.setOnClickListener(this);
        cba.setOnClickListener(this);
        cas.setOnClickListener(this);
        cihm.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onClick(View arg0) {
        String passingword = "";
        switch (arg0.getId()){
        case R.id.ccs:
            passingword = "CCS";
            break;
        case R.id.coe:
            passingword = "COE";
            break;
        case R.id.gn:
            passingword = "GN";
            break;
        case R.id.coed:
            passingword = "COED";
            break;
        case R.id.con:
            passingword = "CON";
            break;
        case R.id.cba:
            passingword = "CBA";
            break;
        case R.id.cas:
            passingword = "CAS";
            break;
        case R.id.cihm:
            passingword = "CIHM";
            break;

        }

        CollegeBulletinListFragment cblf = new CollegeBulletinListFragment();
        Bundle args = new Bundle();
        args.putString("passingWord", passingword);
        cblf.setArguments(args);

         getSupportFragmentManager().beginTransaction()
         .replace(R.id.content_frame, cblf).commit();
    }
}

最佳答案

错误的方法getSupportFragmentManager()方法未定义


因为getSupportFragmentManager()方法在Fragment类中不可用,所以它在FragmentActivity中,因此请使用getActivity()进行访问。

getActivity().getSupportFragmentManager().beginTransaction()
         .replace(R.id.content_frame, cblf).commit();

10-04 23:10
查看更多