2017-03-01 14 views
0

こんにちは、私のプロジェクトは何らかの理由でエミュレータを起動するときにnullpointerを持っているので、理由は分かりません。AndroidスタジオのNullpointer

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

     } 

    } 

Drawableのファイル:デフォルトののstyles.xmlファイルで

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

'getGeneralActionBarBackground'メソッドの残りの部分はどうなりましたか? –

+0

がすべてのコードで更新されました – Kenertj

+2

あなたはActivityまたはAppCompatActivityから拡張していますか?これはAppCompatActivityのgetSupportActionBarで試してみます。 – AndroidRuntimeException

答えて

2

を、何をチェックし、ここで私が取得していますエラーがありますあなたが使っているテーマ。あなたは空のアクティビティを作成する場合、デフォルトのテーマがある:手動のonCreateで、このようなアクションバーを設定する必要があり、その場合には

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

:しかし、あなたはこのような何かを

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

使用している可能性があり:

// 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と適切なテーマを使用する必要があります。