简体   繁体   English

开关盒中有多个碎片

[英]multiple fragment in switch-case

i'm trying to add new fragment in the switch case block. 我正在尝试在switch case块中添加新片段。 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) 错误:(94、45)错误:没有找到适合于replace(int,ProfileFragment)方法FragmentTransaction.replace(int,Fragment,String)的合适方法(实际和正式参数列表的长度不同)方法FragmentTransaction.replace(int ,Fragment)不适用(无法通过方法调用转换将实际参数ProfileFragment转换为Fragment)

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; 在您的情况下,您应该导入android.support.v4.app.Fragment;

The problem is probably because you have imported the fragments from android.app.fragment. 问题可能是因为您已从android.app.fragment导入了片段。 Try to import from the support library. 尝试从支持库导入。 (import android.support.v4.app.Fragment) (导入android.support.v4.app.Fragment)

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

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