getSupportFragmentManager() 不适用于片段 class 的 onCreateView() 方法。

getSupportFragmentManager() is not working on onCreateView() method of the fragment class.

晚上好。我已经在 Whosebug 和一些关于片段的教程文章和网站上阅读了几个问题。我目前正在将第一个片段 class 上的一些数据传递给第二个片段 class。但是我在包中的事务部分遇到了一些错误,它在错误中说 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();
    }
}

error that the method getSupportFragmentManager() method is undefined

因为 getSupportFragmentManager() 方法在 Fragment 中不可用 class 它在 FragmentActivity 中所以使用 getActivity() 访问 it.like:

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

您必须使用支持库 android.support.v4... 才能使用 getSupportFragmentManager()

getSupportFragmentManager() 是在 AppCompatActivity 上定义的,以避免与本机(不支持)getFragmentManager() 冲突。但是,对于 Fragment class 的支持,没有现有的方法可以与之冲突,因此您应该改用:

getFragmentManager()

获取主机 ActivityFragmentManager,或者使用:

getChildFragmentManager()

Fragment 自己的 FragmentManager(用于嵌套 Fragment

有两种Fragment,"regular",进口申报为android.app.Fragment,"support",进口申报为android.support.v4.app.Fragment。您的 Fragment 不是 "support" Fragment,代码中的 import declaration 证明:

package com.example.navigationdrawerexample;

**import android.app.Fragment;**
import android.os.Bundle;

其次,您从 FragmentActivity 访问 getSupportFragmentManager()(因此任何扩展 FragmentActivity 的 class,例如 ActionBarActivityAppCompatActivity)

因此,首先,您应该将导入声明更改为 android.support.v4.app.Fragment

然后,您应该确保承载 FragmentActivityFragmentActivity(或 AppCompatActivity 等)

最后,在您的 Fragment 中,您应该可以调用 getActivity().getSupportFragmentManager()

您还可以浏览 Communicating with Other Fragments, as I would personally execute the code in your question from the Activity, not the Fragment itself, via an interface ( 的文档,找到显示此技术的相关问题。