Reputation: 788
After looking some similar questions I couldn't find why I have the Error inflating class android.support.v7.widget.Toolbar
and how to fix it.
First in my main_activity.java
I imported the two androidx statements as I found on other questions and I added the 2 lines for the toolbar as shown in point 5:
package com.example.tp01_android;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
...
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
final Context context = this;
private ArrayList<String> itemslistArray = new ArrayList<String>();
private EditText newItemTextField;
private ArrayAdapter simpleAdapter;
private FloatingActionButton fab;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ------------------------------- TOOLBAR -------------------------------------------------
**Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);**
}
...
Now in my main_activity.xml
I added my toolbar following the point 4
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="auto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.98"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.389"
app:srcCompat="@android:drawable/ic_input_add" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Custom list:"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listSimple" />
<ListView
android:id="@+id/listSimple"
android:layout_width="397dp"
android:layout_height="211dp"
android:gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText">
</ListView>
<ListView
android:id="@+id/customListView"
android:layout_width="408dp"
android:layout_height="294dp"
android:gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2">
</ListView>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Simple list:" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
What did I make wrong or what should I change ?
Upvotes: 0
Views: 66
Reputation: 2560
Your Toolbar
import is wrong. Use the androidx
one instead of support lib version.
In your xml use androidx.appcompat.widget.Toolbar
instead of android.support.v7.widget.Toolbar
See this for more info.
The guide you pointed hasn't been updated and is using the old support artifacts. Shift to the androidx.*
version of toolbar as you have done with other components.
Upvotes: 1
Reputation: 163
Use androidx.appcompat.widget.Toolbar
instead of android.support.v7.widget.Toolbar
in your main_activity.xml
Upvotes: 1