【问题标题】:setBackgroundDrawable is throwing NullPointerException in creating Android Custom ActionBarsetBackgroundDrawable 在创建 Android 自定义 ActionBar 时抛出 NullPointerException
【发布时间】:2015-02-17 15:42:38
【问题描述】:

在创建自定义操作栏时,我在 setBackgroundDrawable 上遇到 NullPointerException。 受保护的无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    ActionBar actionBar = getActionBar();
    Bitmap b = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);

    actionBar.setBackgroundDrawable(new BitmapDrawable(getResources(), b));
    actionBar.setCustomView(R.layout.actionbar_view);

LogCat结果如下:

Caused by: java.lang.NullPointerException
        at actionbarcustomview.practice.nkd.actionbarcustomviews.MainActivity.onCreate(MainActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:4465)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)

第 30 行是

actionBar.setBackgroundDrawable(new BitmapDrawable(getResources(), b));

我正在使用 Android Studio 并且配置是

    defaultConfig {
    applicationId "actionbarcustomview.practice.nkd.actionbarcustomviews"
    minSdkVersion 17
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}

请帮我解决这个问题。

【问题讨论】:

  • 你为什么不直接记录 actionBar 的值?

标签: android exception android-actionbar


【解决方案1】:

这是一篇应该有所帮助的博文:

http://blog.perpetumdesign.com/2011/08/strange-case-of-dr-action-and-mr-bar.html

问题是ActionBar actionBar = getActionBar(); 返回null

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多