【问题标题】:getSupportFragmentManager() is not working on onCreateView() method of the fragment class.getSupportFragmentManager() 不适用于片段类的 onCreateView() 方法。
【发布时间】:2015-11-11 15:33:50
【问题描述】:

晚安。我在 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();
    }
}

【问题讨论】:

    标签: java android android-fragments bundle


    【解决方案1】:

    getSupportFragmentManager()方法未定义的错误

    因为getSupportFragmentManager() 方法在Fragment 类中不可用,所以它在FragmentActivity 中,所以使用getActivity() 来访问它。like:

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

    【讨论】:

    • 所以我必须将上面的扩展类更改为FragmentActivity?
    • @JohnRannielSinel:不,请参阅我的编辑答案。使用getActivity()
    • @JohnRannielSinel:如果你想使用getSupportFragmentManager() 方法,那么还要从android.support.v4.app.Fragment 导入片段而不是android.app.Fragment
    • getActivity() 方法中没有可用的方法 getSupportFragmentManager()。
    • @JohnRannielSinel:你是在添加Fragment的Activity中扩展Activity还是FragmentActivity?
    【解决方案2】:

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

    getFragmentManager()

    要获得主机ActivityFragmentManager,或者,使用:

    getChildFragmentManager()

    Fragment自己的FragmentManager(用于嵌套Fragments)

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • @Kevinrob getSupportFragmentManager() 来自FragmentActivity 而不是Fragment
      【解决方案4】:

      Fragment 有两种类型,“regular”,其导入声明为android.app.Fragment,以及“support”,即android.support.v4.app.Fragment。您的 Fragment 不是“支持”Fragment,您的代码中的 import declaration 就是证明:

      package com.example.navigationdrawerexample;
      
      **import android.app.Fragment;**
      import android.os.Bundle;
      

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

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

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

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

      您还可以浏览Communicating with Other Fragments 的文档,因为我会亲自从Activity 而不是Fragment 本身通过接口执行您问题中的代码(here's a link 到显示这种技术)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多