在onResume中设置操作栏时出错

时间:2014-11-11 06:41:26

标签: android

在onResume中尝试设置操作栏的活动方法是我的setActionbar

代码
protected void setActionBar() {

    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View customActionBar = inflater.inflate(R.layout.actionbar_layout, null);
    ivDrawerIconActionbar = (ImageView) customActionBar.findViewById(R.id.iv_drawer_icon);
    tvAppName = (TextView) customActionBar.findViewById(R.id.tv_app_name);
    ivAppLogo = (ImageView) customActionBar.findViewById(R.id.iv_app_logo);
    actionBar.setCustomView(customActionBar);
    ll_title = (LinearLayout)customActionBar.findViewById(R.id.ll_title);
    ivDrawerIconActionbar.setOnClickListener(this);

    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#B44041")); 
    actionBar.setBackgroundDrawable(colorDrawable);

    mDrawerLayout.setDrawerListener(new DrawerListener() {

        @Override
        public void onDrawerStateChanged(int arg0) {

        }

        @Override
        public void onDrawerSlide(View arg0, float arg1) {

        }

        @Override
        public void onDrawerOpened(View arg0) {
            ll_title.setVisibility(View.VISIBLE);
        }

        @Override
        public void onDrawerClosed(View arg0) {
            ll_title.setVisibility(View.GONE);
        }
    });

}

这是onresume方法,这里调用setactionbar()

@Override
protected void onResume() {
    setActionBar();

    super.onResume();
}

这里添加了堆栈跟踪以进行崩溃。应用程序崩溃发生在setActionBar方法中请告诉我为什么崩溃正在发生。英语

2 个答案:

答案 0 :(得分:1)

使用

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

而不是

actionBar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);

如果设置了我提到的显示选项,将使用您的自定义视图。

答案 1 :(得分:1)

setNavigationMode的可能值为:

  • NAVIGATION_MODE_STANDARD
  • NAVIGATION_MODE_LIST
  • NAVIGATION_MODE_TABS

删除此内容:actionBar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);

您只需使用actionBar.setDisplayShowCustomEnabled(true);

在操作栏中设置自定义视图即可