简体   繁体   English

错误的第二个参数类型。 必需:“ android.support.v4.app.Fragment”

[英]wrong 2nd argument type. required: 'android.support.v4.app.Fragment'

The error im getting is: Wrong 2nd arguement type. 我得到的错误是:错误的第二争论类型。 found: 'com.example.appname.MainmenuFragment', required: 'android.support.v4.app.Fragment' By the way I know a similar question has been asked but that case is slightly different because this code is written in MainmenuActivity which is an activity not a fragment and MainmenuFragment is a fragment as the name implies. 找到:“ com.example.appname.MainmenuFragment”,必需:“ android.support.v4.app.Fragment”顺便说一句,我知道有人问过类似的问题,但是这种情况略有不同,因为此代码是用MainmenuActivity编写的,顾名思义,是一个活动而不是一个片段,而MainmenuFragment是一个片段。 It worked this morning. 今天早上工作了。 I've been exploring quite a lot today but ended up having this unusual error. 我今天进行了很多探索,但最终遇到了这个异常错误。 Here is the code in which I am getting the error: 这是我收到错误的代码:

public class MainmenuActivity extends AppCompatActivity {


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_mainmenu:
                    transaction.replace(R.id.container, new MainmenuFragment()).commit();
                    return true;
}
return false;
};

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainmenu);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.container, new MainmenuFragment()).commit();

    }}

the line 线

transaction.replace(R.id.container, new MainmenuFragment()).commit();

and

transaction.replace(R.id.container, new MainmenuFragment()).commit();

is where I am getting the error. 是我得到错误的地方。 To be honest if I try using other fragment files instead of Mainmenu_Fragment it works fine wierdly. 老实说,如果我尝试使用其他片段文件而不是Mainmenu_Fragment,它将可以很好地工作。

您可能在MainmenuFragment使用了android.app.Fragment而不是android.support.v4.app.Fragment

The problem might be because of getSupportFragmentManager() . 问题可能是由于getSupportFragmentManager() Try changing it to getFragmentManager() . 尝试将其更改为getFragmentManager()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误的第二个参数,找到:“ com.test.me”,使用替换(带有片段事务)时需要“ android.support.v4.app.Fragment” - Wrong 2nd Argument, Found: “com.test.me”, required “android.support.v4.app.Fragment” when using replace (with fragment transaction) 必需的android.support.v4.app.fragment - required android.support.v4.app.fragment 错误的第二个参数类型。 找到“java.lang.String”,需要“android.location.Location” - Wrong 2nd argument type. Found 'java.lang.String', required 'android.location.Location' 类型不匹配:无法从android.support.v4.app.Fragment转换为android.app.Fragment - Type mismatch: cannot convert from android.support.v4.app.Fragment to android.app.Fragment 错误的第二个参数类型带有片段 - Wrong 2nd argument type with fragment 有没有一种方法可以将片段转换为android.support.v4.app.Fragment - Is there a way to convert fragment to android.support.v4.app.Fragment 为什么此错误显示“不兼容的类型”。“必需:android.support.v4.app.Fragment”? - Why is this Error showing “Incompatible Types.” “Required: android.support.v4.app.Fragment”? 片段错误:所需的类型不兼容:android.support.v4.app.Fragment,找到:package_name.app_name.Fragment_name - Fragment Error: Incompatible Types Required: android.support.v4.app.Fragment, Found: package_name.app_name.Fragment_name 覆盖 android.support.v4.app.fragment 中已弃用的方法 - Override deprecated method in android.support.v4.app.fragment 在实现FragmentTabHost时将android.support.v4.app.Fragment强制转换为android.app.Fragment的可能性 - Possibility of casting android.support.v4.app.Fragment to android.app.Fragment when implementing a FragmentTabHost
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM