不兼容的类型:HomeFragment无法在Android中转换为Fragment
我在这部分代码中收到错误:
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");
}
}
我得到
错误:不兼容类型:HomeFragment无法转换为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;
这是班级名称:
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.
这似乎是 import
问题。
使用时getFragmentMangager()
,确保您的 Fragment
类扩展 android.app.Fragment
类。
When using getFragmentMangager()
, make sure that your Fragment
classes extend android.app.Fragment
class.
如果您有任何机会使用 android.support.v4.app.Fragment
(请参阅您的导入),然后你需要使用 getSupportFragmentManager()
而不是
If by any chance you are using android.support.v4.app.Fragment
(see your imports), then you need to use getSupportFragmentManager()
instead
希望它有所帮助