如何从onNavigationItemSelected方法启动FragmentActivity

时间:2017-09-01 19:38:55

标签: android android-fragments android-fragmentactivity android-navigation-drawer

我使用了以下段来event_attendee GET /event_attendees/:id(.:format) event_attendees#show PATCH /event_attendees/:id(.:format) event_attendees#update PUT /event_attendees/:id(.:format) event_attendees#update DELETE /event_attendees/:id(.:format) event_attendees#destroy 替换FrameLayout Fragment方法,

onNavigationItemSelected

我想用public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); Fragment fragment; if (id == R.id.nav_camara) { fragment = new MyFragment(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.mainFrame, fragment); ft.commit(); } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } } 替换FrameLayout,然后我做了

FragmentActivity

但在FragmentActivity fragmentAc; if (id == R.id.nav_camara) { fragmentAc= new MyFragmentActivity(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.mainFrame, fragmentAc); ft.commit(); }

上获得Wrong 2nd argument type. Found: 'android.support.v4.app.FragmentActivity', required: 'android.app.Fragment'

我现在应该改变什么,帮帮我

1 个答案:

答案 0 :(得分:0)

您正试图将FragmentActivity传递到需要Fragment的位置,这就是错误所在。

如果您想要打开其他活动而不是替换片段,则必须致电startActivity(...)

取代:

if (id == R.id.nav_camara) {
     fragmentAc= new MyFragmentActivity();
     FragmentTransaction ft = getFragmentManager().beginTransaction();
     ft.replace(R.id.mainFrame, fragmentAc);
     ft.commit();
 }

使用:

if (id == R.id.nav_camara) {
    startActivity(new Intent(getActivity(), MyFragmentActivity.class));
}