Java 不兼容的类型:Android 中 HomeFragment 无法转换为 Fragment

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27037662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:43:13  来源:igfitidea点击:

incompatible types: HomeFragment cannot be converted to Fragment in Android

javaandroidandroid-fragments

提问by Lior

I'm getting an error in this part of code:

我在这部分代码中遇到错误:

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment =new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
            break;

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

I get

我得到

error: incompatible types: HomeFragment cannot be converted to Fragment

错误:不兼容的类型:HomeFragment 无法转换为 Fragment

this is the imports:

这是进口:

package liorsiag.lgbt;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;

and this is the class title:

这是课程标题​​:

public class MainActivity extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

No matter what I've tried I still get this error

无论我尝试过什么,我仍然收到此错误

I've tried a lot of navigation drawer tutorials, but none of them seem to work.

我已经尝试了很多导航抽屉教程,但它们似乎都不起作用。

采纳答案by zozelfelfo

This seems to be an importproblem.

这似乎是一个import问题。

When using getFragmentMangager(), make sure that your Fragmentclasses extend android.app.Fragmentclass.

使用时getFragmentMangager(),请确保您的Fragment类扩展了android.app.Fragment类。

If by any chance you are using android.support.v4.app.Fragment(see your imports), then you need to use getSupportFragmentManager()instead

万一你正在使用android.support.v4.app.Fragment(请参见进口),那么你需要使用getSupportFragmentManager()替代

Hope it helps

希望能帮助到你

回答by ramuta

Try changing

尝试改变

import android.app.Fragment;

import android.app.Fragment;

to

import android.support.v4.app.Fragment;

import android.support.v4.app.Fragment;

Use classes from that support lib for all other imports too. Also getSupportFragmentManager()as mentioned in the other answer.

对所有其他导入也使用该支持库中的类。也getSupportFragmentManager()如其他答案中所述。

回答by Ammar Rajab

In your HomeFragmentclass

在你的HomeFragment班级

replace:

代替:

import android.app.Fragment;

with:

和:

import android.support.v4.app.Fragment;

回答by Harunduet

In my case i have changed line-1 with line-2

就我而言,我已将第 1 行更改为第 2 行

Line-1: import android.app.Fragment;

线路1:import android.app.Fragment;

Line-2: import android.support.v4.app.Fragment;

线-2:import android.support.v4.app.Fragment;

Its working

它的工作

回答by Sai Gopi N

use getSupportFragmentManager()Instead of getFragmentManager()

使用getSupportFragmentManager()代替getFragmentManager()

getSupportFragmentManager()
    .beginTransaction()
    .replace(in.jama.app.R.id.container, new Fragment())
    .commit();

回答by Negatu

import android.app.Fragment;works with getFragmentManager()method but before you have to remove the import android.support.v4.app.Fragment;

导入android.app.Fragment;getFragmentManager()方法一起使用,但在您必须删除导入之前android.support.v4.app.Fragment;

回答by Mehul Raj

you just have to import android.support.v4.app.Fragment; in the all the FragmentClass();. that's it.

你只需要导入 android.support.v4.app.Fragment; 在所有的 FragmentClass(); 中。就是这样。

回答by Faizan Khan

In Android Studio 2.3 getSupportFragmentManager works with android.support.v4.app but android studio 3.1 you have to use getFragmentManager enter image description here

在 Android Studio 2.3 中 getSupportFragmentManager 与 android.support.v4.app 一起使用,但在 android studio 3.1 中你必须使用 getFragmentManager在此处输入图像描述

回答by Shripada

If you are using support library, you should ensure to import both Fragment and FragmentManager from the support library. You will also need to ensure to load the support fragment manager.

如果您使用支持库,则应确保从支持库中导入 Fragment 和 FragmentManager。您还需要确保加载支持片段管理器。

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
//other imports here...

public class SomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_some);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);

        if(fragment == null) {
            fragment = new SomeFragment();
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }

    }
}