zoukankan      html  css  js  c++  java
  • The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, SettingFragment, String)

    The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, SettingFragment, String)

    引发错误的原因是因为import包的时候Fragment是引用的V4的,而activity继承Activity。
    getFragmentManager()是activity中的方法
    但是getSupportFragmentManager是FragmentActivity中的方法
    使用要统一

    最后

    package com.example.fragment;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    
    public class MainActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }
        }
    }
    package com.example.fragment;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    
    /**
     * A placeholder fragment containing a simple view.
     */
    public class PlaceholderFragment extends Fragment {
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            rootView.findViewById(R.id.btnshow).setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    getFragmentManager().beginTransaction().replace(R.id.container, new AnotherFragment()).commit();
                }
            });
            return rootView;
        }
    }
    stareblankly.cn
  • 相关阅读:
    Day 29 作业/服务端实现接收客户端的连接并发
    Day 28 操作系统发展/进程
    Day 27 作业(FTP)
    Day 27 subprocess模块/粘包问题以及解决/UDP协议/socketserver
    Day 26 作业
    Day 26 互联网协议/Socket套接字
    Day 22 绑定方法和非绑定方法/isinstance/issubclass/反射/魔法方法/单例模式
    Day 21 作业
    Day 21 组合/封装/property特性/多态和多态性/抽象类/鸭子类型
    day 20 作业
  • 原文地址:https://www.cnblogs.com/stareblankly/p/4900051.html
Copyright © 2011-2022 走看看