【问题标题】:custom action bar image - app crashes自定义操作栏图像 - 应用程序崩溃
【发布时间】:2016-07-16 16:29:09
【问题描述】:

您好,我无法在操作栏上设置自定义背景。我希望在动作栏上的每一个意图自定义图像。

我已经试过了

final ActionBar actionBar = getActionBar();
    BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.my_picture));
    actionBar.setBackgroundDrawable(background);

但应用程序因错误而崩溃

          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lowrider.menuapp/com.example.lowrider.menuapp.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference

【问题讨论】:

  • 您可能需要致电getSupportActionBar() 而不是getActionBar()
  • 你说得对

标签: android image action


【解决方案1】:

您应该使用 getSupportActionBar() 而不是 getActionBar()。

BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.my_picture));
getSupportActionBar().setBackgroundDrawable(background);

【讨论】:

    猜你喜欢
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多