setSupportActionBar (androidx.appcompat.widget.Toolbar) in AppCompatActivity cannot be applied

前端 未结 8 1467
半阙折子戏
半阙折子戏 2020-12-07 05:36

An error showing incompatible types Android widget toolbar cannot be converted in Java compiler while working on Android Studio.

    Toolbar toolbar = (Toolb         


        
相关标签:
8条回答
  • 2020-12-07 06:13

    You can invoke ActivityCompat#requireViewById which allows to omit the type cast and returns a @NonNull Toolbar reference:

    Toolbar toolbar = ActivityCompat.requireViewById(this, R.id.toolbar);
    setSupportActionBar(toolbar);
    

    The title can be set on the @Nullable ActionBar reference returned by AppCompatActivity#getSupportActionBar:

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle("GPS PRESENCE SYSTEM");
    }
    
    0 讨论(0)
  • 2020-12-07 06:16

    You are using androidx and Android both at the same time. That is throwing an error. Either use androidx or use android appcompat.

    0 讨论(0)
提交回复
热议问题