material-components/material-components-android-codelabs

MDC-102 ProductGridFragment.java imports wrong Toolbar library

acutetech opened this issue · 2 comments

The instructions clearly state "When importing the Toolbar class, make sure you import androidx.appcompat.widget.Toolbar and not android.widget.Toolbar"

Yet the code shows:
import android.widget.Toolbar;

The finished-codelab code is correct.

Note: I'm a beginner to java and android dev as well as github, so let me know if said anything wrong or posted in the wrong format.

I was getting an error "error: incompatible types: android.widget.Toolbar cannot be converted to androidx.appcompat.widget.Toolbar" while building the app.

The code snippet where the error was shown is here:
private void setUpToolbar(View view) {
Toolbar toolbar = view.findViewById(R.id.app_bar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
activity.setSupportActionBar(toolbar);
}
}

"Toolbar toolbar = view.findViewById(R.id.app_bar);" seems to call the android.widget.Toolbar library which cannot be type casted to the androidx.appcompat.widget.Toolbar library in the "activity.setSupportActionBar(toolbar);" line.

I fixed it by initiating the toolbar as "androidx.appcompat.widget.Toolbar toolbar = view.findViewById(R.id.app_bar)" but this seems to cause the toolbar to turn white instead of blue as shown in the picture. Is this a possible fix and what can be done about the color?

It is now November 7, 2020 --- why hasn't this mistake been corrected.

Should I even trust the rest of these tutorials .

We should not have to be learning by finding mistakes in "tutorials" meant to teach us capabilities.

Very disappointing!