用菜单替换片段

时间:2014-03-17 22:03:42

标签: android replace

当点击菜单按钮时,我想要替换片段。显示吐司,但似乎我有FragmentTransaction的问题,因为它告诉我它

  

描述资源路径位置类型   无法实例化类型FragmentTransaction ActivityAppLaunch.java / juraQuiz / src / com / pthuermer / juraquiz第71行Java问题

这是我试过的:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.actionbar_menu_info) {
        Toast.makeText(getApplicationContext(), "Info", Toast.LENGTH_LONG).show();

        FragmentTransaction ft = new FragmentTransaction();
        Fragment fragment = new FragmentInfo();
        ft.replace(R.id.container, fragment);

    }
    if (id == R.id.actionbar_menu_settings) {
        Toast.makeText(getApplicationContext(), "Settigns", Toast.LENGTH_LONG).show();
    }
    return super.onOptionsItemSelected(item);
}

1 个答案:

答案 0 :(得分:0)

尝试FragmentManager

FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new FragmentInfo();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();