我正在尝试在Android中创建导航抽屉,但我遇到了一些问题。我的MainActivity.java中有一个方法可以处理我在导航抽屉上的点击,并指导用户进行适当的活动。方法如下所示:
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
android.app.Fragment objFragment = null;
switch (position){
case 0:
objFragment= new menu1_Fragment();
break;
case 1:
objFragment = new menu2_Fragment();
break;
case 2:
objFragment = new menu3_Fragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, objFragment)
.commit();
}
我的问题是我在.replace(R.id.container, objFragment)
的第三行到最后一行收到错误:FragmentTransaction cannot be applied to (int, android.app.fragment)
答案 0 :(得分:5)
如果您使用SupportFragmentManager
http://developer.android.com/reference/android/support/v4/app/Fragment.html http://developer.android.com/reference/android/support/v4/app/FragmentManager.html
VS
http://developer.android.com/reference/android/app/FragmentManager.html http://developer.android.com/reference/android/app/Fragment.html
你永远不能在两者之间混合搭配
// update the main content by replacing fragments
android.support.v4.app.Fragment objFragment = null;
switch (position){
case 0:
清晰度,替换:
android.app.Fragment objFragment = null;
与
android.support.v4.app.Fragment objFragment = null;