gpt4 book ai didi

java - Android Studio 中的空指针

转载 作者:行者123 更新时间:2023-12-02 02:59:34 24 4
gpt4 key购买 nike

您好,由于某种原因,我的项目在启动模拟器时有一个空指针,我不确定原因是什么。这是我收到的错误:

W/System.err: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference
W/System.err: at com.panda.cleaner_batterysaver.ui.MainActivity.applyKitKatTranslucency(MainActivity.java:201)
W/System.err: at com.panda.cleaner_batterysaver.ui.MainActivity.onCreate(MainActivity.java:83)

代码部分:

private void applyKitKatTranslucency() {

// KitKat translucent navigation/status bar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager mTintManager = new SystemBarTintManager(this);
mTintManager.setStatusBarTintEnabled(true);
mTintManager.setNavigationBarTintEnabled(true);
// mTintManager.setTintColor(0xF00099CC);

mTintManager.setTintDrawable(UIElementsHelper
.getGeneralActionBarBackground(this));

getActionBar().setBackgroundDrawable(
UIElementsHelper.getGeneralActionBarBackground(this));

}

}

调用该方法的可绘制文件:

  public class UIElementsHelper {

private static final String NOW_PLAYING_COLOR = "NOW_PLAYING_COLOR";
private static final String BLUE = "BLUE";
private static final String RED = "RED";
private static final String GREEN = "GREEN";
private static final String ORANGE = "ORANGE";
private static final String PURPLE = "PURPLE";
private static final String MAGENTA = "MAGENTA";
private static final String GRAY = "GRAY";
private static final String WHITE = "WHITE";
private static final String BLACK = "BLACK";

/**
* Returns the ActionBar color based on the selected color theme (not used
* for the player).
*/
public static Drawable getGeneralActionBarBackground(Context context) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);

Drawable drawable = new ColorDrawable(0xFF9acd32);


return drawable;

}

}

最佳答案

在默认的 styles.xml 文件中,检查您正在使用的主题。如果您创建空 Activity ,则默认主题是:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

但是,您可能使用过如下内容:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

在这种情况下,您需要在 onCreate 中手动设置操作栏:

// Assuming you have a toolbar in your xml layout with id toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setBackgroundDrawable(
UIElementsHelper.getGeneralActionBarBackground(this));
}

如果您不使用 AppCompat,那么您将需要使用 getActionBar 和适当的主题。

关于java - Android Studio 中的空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com