Skip to content Skip to sidebar Skip to footer

Setsupportactionbar Cannot Be Applied To Android.support.v7.widget.toolbar

On this new project, when setting a toolbar into the main activity java file, it gets the error on 'setSupportActionBar(mToolbar)' saying 'getSupportActionBar() in AppCompatAct

Solution 1:

Looks like it's a typo, and you're calling getSupportActionBar() (with a g) instead of setSupportActionBar().

Solution 2:

You need to use setSupportActionBar() to configure toolbar in the activity:

publicclassMainActivityextendsAppCompatActivity {

    private Toolbar mToolbar;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar =  findViewById(R.id.main_page_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("WhatsApp");
    }
}

Post a Comment for "Setsupportactionbar Cannot Be Applied To Android.support.v7.widget.toolbar"