2017-03-01 4 views
0

Hallo so mein Projekt ist mit einem Nullpointer beim Start auf Emulator aus irgendeinem Grund, ich bin mir nicht sicher, was der Grund ist. Hier ist der Fehler Ich erhalte:Nullpointer in Android Studio

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 Teile:

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 Datei, in der die Methode aufgerufen wird:

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; 

    } 

} 
+0

Was ist mit dem Rest der 'getGeneralActionBarBackground' Methode passiert? –

+0

mit dem gesamten Code aktualisiert – Kenertj

+2

Erweitern Sie von Activity oder AppCompatActivity? Es ist von AppCompatActivity versuchen mit getSupportActionBar. – AndroidRuntimeException

Antwort

2

In Ihrer Standard-styles.xml Datei, zu überprüfen, was Thema, das Sie verwenden. Die Standard-Theme, wenn Sie eine leere Aktivität erstellen sind:

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

Allerdings können Sie so etwas wie diese verwendet haben:

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

, in dem Fall, dass Sie manuell die Aktionsleiste wie dies in onCreate festlegen müssen :

// 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)); 
} 

Wenn Sie AppCompat nicht verwenden, müssen Sie getActionBar und ein entsprechendes Thema verwenden.

Verwandte Themen