简体   繁体   中英

multiple fragment in switch-case

i'm trying to add new fragment in the switch case block. I used the same code for add inbox fragment. But i have this error:

Error:(94, 45) error: no suitable method found for replace(int,ProfileFragment) method FragmentTransaction.replace(int,Fragment,String) is not applicable (actual and formal argument lists differ in length) method FragmentTransaction.replace(int,Fragment) is not applicable (actual argument ProfileFragment cannot be converted to Fragment by method invocation conversion)

I think that the problem is related to the library but i don't know how to fix it.

This is my code:

case R.id.home:
    //Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_SHORT).show();
    HomeFragment home = new HomeFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame, home);
    fragmentTransaction.commit();
    return true;
case R.id.profilo:
    //Toast.makeText(getApplicationContext(),"Profilo",Toast.LENGTH_SHORT).show();
    ProfileFragment profilo = new ProfileFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
    fragmentTransaction1.replace(R.id.frame, profilo);
    fragmentTransaction1.commit();
    return true;

Please help me!

Check the import in your fragment class that whether you have imported

import android.support.v4.app.Fragment;

or

import android.app.Fragment;

In your case you should import android.support.v4.app.Fragment;

The problem is probably because you have imported the fragments from android.app.fragment. Try to import from the support library. (import android.support.v4.app.Fragment)

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