Reputation: 57
Hi so my project is having a nullpointer when starting on emulator for some reason, i am not sure what the reason is. Here is the error i am getting :
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)
Code parts:
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));
}
}
Drawable file where the method is called :
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;
}
}
Upvotes: 0
Views: 74
Reputation: 3931
In your default styles.xml file, check what theme you are using. The default theme if you create an empty Activity is:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
However, you may have used something like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
in which case you need to manually set the action bar like this in 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));
}
If you are not using AppCompat then you will need to use getActionBar and an appropriate theme.
Upvotes: 2