Search code examples
androidnullpointerexceptionandroid-toolbarandroid-appbarlayout

Change Toolbar & AppBarLayout Background onCreate


I'm trying to have 2 theme options for my app, Dark and Light. When the dark theme is selected I use sharedpreference to save and apply the theme on startup. But when I try to change the background color of the toolbar & appBarLayout like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    long currentTheme = sharedPref.getInt(getString(string.current_theme), 0);
    if (currentTheme == 0) {
        this.setTheme(R.style.AppTheme_NoActionBar);
    }
    if (currentTheme == 1) {
        this.setTheme(R.style.AppTheme_NoActionBar_Dark);
        Toolbar toolbar = (Toolbar) findViewById(id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setBackgroundColor(getResources().getColor(ActionbarDark));
    }

Logcat returns the error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setBackgroundColor(int)' on a null object reference

And I cant understand why

Update: Fixed it by using the answer from @Nilesh Rathod and changing the theme of the activity before setting the content view, then after setting content view changing the theme of the Toolbar and AppBarLayout.


Solution

  • You have missed R in findViewById of toolbar

    it should like below

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    

    change your code like this

    Toolbar toolbar;
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        long currentTheme = sharedPref.getInt(getString(string.current_theme), 0);
        if (currentTheme == 0) {
            this.setTheme(R.style.AppTheme_NoActionBar);
    
        }
        if (currentTheme == 1) {
            this.setTheme(R.style.AppTheme_NoActionBar_Dark);
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff00DDED));
        }
        setContentView(R.layout.activity_main);
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    
    
        android.app.FragmentManager fragmentmanager = getFragmentManager();
        fragmentmanager.beginTransaction()
                .replace(R.id.content_frame
                        , new FirstFragment())
                .commit();
    
        DrawerLayout drawer = (DrawerLayout) findViewById(id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, string.navigation_drawer_open, string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
    
        NavigationView navigationView = (NavigationView) findViewById(id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.getMenu().getItem(0).setChecked(true);
    }