简体   繁体   中英

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. 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.

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

The problem might be because of getSupportFragmentManager() . Try changing it to getFragmentManager() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM