问题描述
我在这部分代码中遇到错误:
private void displayView(int position) {//通过替换片段更新主要内容片段片段=空;开关(位置){案例0:片段 = 新的 HomeFragment();休息;情况1:片段 = 新 FindPeopleFragment();休息;案例2:片段 = 新照片片段();休息;案例3:fragment = new CommunityFragment();休息;案例4:fragment = new PagesFragment();休息;案例5:片段 = 新 WhatsHotFragment();休息;默认:休息;}如果(片段!= null){FragmentManager fragmentManager = getFragmentManager();fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();//更新选中的项目和标题,然后关闭抽屉mDrawerList.setItemChecked(position, true);mDrawerList.setSelection(position);setTitle(navMenuTitles[位置]);mDrawerLayout.closeDrawer(mDrawerList);} 别的 {//创建片段时出错Log.e("MainActivity", "创建片段时出错");}}
我明白
错误:类型不兼容:无法将 HomeFragment 转换为 Fragment
这是进口:
package liorsiag.lgbt;导入 android.app.FragmentManager;导入 android.content.res.Configuration;导入 android.content.res.TypedArray;导入 android.os.Bundle;导入 android.support.v4.app.ActionBarDrawerToggle;导入 android.app.Fragment;导入 android.support.v4.app.FragmentActivity;导入 android.support.v4.widget.DrawerLayout;导入 android.util.Log;导入 android.view.Menu;导入 android.view.MenuItem;导入 android.view.View;导入 android.widget.AdapterView;导入 android.widget.ListView;导入 java.util.ArrayList;
这是课程标题:
public class MainActivity extends FragmentActivity {私人 DrawerLayout mDrawerLayout;私有 ListView mDrawerList;私人 ActionBarDrawerToggle mDrawerToggle;
无论我尝试过什么,我仍然收到此错误
我尝试了很多导航抽屉教程,但似乎都没有效果.
这似乎是一个 import
问题.
在使用 getFragmentMangager()
时,请确保您的 Fragment
类扩展了 android.app.Fragment
类.
如果您正在使用 android.support.v4.app.Fragment
(请参阅您的导入),那么您需要使用 getSupportFragmentManager()
代替>
希望能帮到你
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
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.
This seems to be an import
problem.
When using getFragmentMangager()
, make sure that your Fragment
classes extend android.app.Fragment
class.
If by any chance you are using android.support.v4.app.Fragment
(see your imports), then you need to use getSupportFragmentManager()
instead
Hope it helps
这篇关于不兼容的类型:Android 中 HomeFragment 无法转换为 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!