简体   繁体   中英

android app Toolbar.setTitle error Attemp to invoke virtual method 'void.androidx.appcompat.widget'

Trying to create android simple app using this code:

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("mytitle");

and this

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("mytitle");
setSupportActionBar(toolbar);

in Android X both of them can't use, please help how to set title toolbar?

I think you are importing the wrong toolbar class, or rather two different toolbar classes in xml and java code

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("mytitle");

This code should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM