2 kevinjqy kevinjqy 于 2014.11.07 11:09 提问

actionbar里添加发布和搜索按钮,但点击发布按钮无法跳转到相关fragment

public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
//Fragment fragment = null;
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

// Handle action buttons
switch(item.getItemId()) {
case R.id.action_release:
{

        Toast.makeText(this, R.string.action_release, Toast.LENGTH_SHORT).show();
        ReleaseFragment fragment2 = new ReleaseFragment();
        Bundle args2 = new Bundle();
        args2.putString("name", "发布"); 
        fragment2.setArguments(args2);
        FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
        transaction2.replace(R.id.realtabcontent,fragment2);
        //transaction2.addToBackStack(null);
        transaction2.commit();
        return true;
    }
    case R.id.action_search:
    {

         //Toast.makeText(this, R.string.action_search, Toast.LENGTH_SHORT).show();
        SearchFragment fragment1 = new SearchFragment();
        Bundle args1 = new Bundle();
        args1.putString("name", "搜索"); 
        fragment1.setArguments(args1);
        FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
        transaction1.replace(R.id.realtabcontent,fragment1);
        //transaction1.addToBackStack(null);
        transaction1.commit();
        return true;
    }

    default:
        return super.onOptionsItemSelected(item);

    }          
}

点击发布按钮会弹出toast提示,但是内容空白。点击搜索按钮会正确的显示内容和控件。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!