• 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
  • 相关阅读:
    MySQL 对于千万级的大表要怎么优化?
    随便写的一些docker使用操作命令
    零基础学python大概要多久?我用了30天
    普通人学python有意义吗?意义重大
    华为私有云组件
    Mysql 调优(二)分析思路
    MySQL 调优(一)调优原则
    shell脚本获取当前时间,分钟之前时间、小时之前时间和天之前时间
    java_windows环境变量自动设置脚本
    plsql中文乱码问题解决方案
  • 原文地址:https://www.cnblogs.com/stareblankly/p/4900051.html
Copyright © 2020-2023  润新知