簡體   English   中英

在onResume中設置操作欄時出錯

[英]Error in setting actionbar in onResume

在嘗試設置actionbar的活動的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();
}

在這里添加stacktrace崩潰。 setActionBar方法中發生應用程序崩潰請告訴我為什么發生崩潰。

采用

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

代替

actionBar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);

如果設置了我提到的顯示選項,則將使用您的自定義視圖。

setNavigationMode可能值為:

  • NAVIGATION_MODE_STANDARD
  • NAVIGATION_MODE_LIST
  • NAVIGATION_MODE_TABS

刪除此內容: actionBar.setNavigationMode(ActionBar.DISPLAY_SHOW_CUSTOM);

您可以使用actionBar.setDisplayShowCustomCustomEnabled actionBar.setDisplayShowCustomEnabled(true);在Actionbar中簡單地設置“自定義”視圖actionBar.setDisplayShowCustomEnabled(true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM