gpt4 book ai didi

java - 不兼容的类型 : HomeFragment cannot be converted to Fragment in Android

转载 作者:IT老高 更新时间:2023-10-28 21:04:28 25 4
gpt4 key购买 nike

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

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");
}
}

我明白了

error: incompatible types: HomeFragment cannot be converted to Fragment

这是进口:

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;

这是类(class)名称:

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

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

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

最佳答案

这似乎是 import问题。

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

如果您使用 android.support.v4.app.Fragment (查看您的导入),那么您需要使用 getSupportFragmentManager()而是

希望对你有帮助

关于java - 不兼容的类型 : HomeFragment cannot be converted to Fragment in Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27037662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com