java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]


2021-04-18 15:18:12.620 5902-5902/com.example.myapplication E/AndroidRuntime: 

 FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 5902
    java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{com.example.myapplication/ com.example.myapplication.MainActivity}: 
    android.view.InflateException: Binary XML file        
    line #9 in com.example.myapplication:layout/ activity_main: 
    Binary XML file line #9 in com.example.myapplication:layout/activity_main: 
    Error inflating class android.support.v7.widget.Toolbar
 Caused by: android.view.InflateException: Binary XML file line #9 in 
 com.example.myapplication:layout/activity_main: 
Binary XML file line #9 in com.example.myapplication:layout/activity_main: 
Error inflating class android.support.v7.widget.Toolbar
     Caused by: android.view.InflateException:
 Binary XML file line #9 in com.example.myapplication:layout/activity_main: 
Error inflating class android.support.v7.widget.Toolbar

 Caused by: java.lang.ClassNotFoundException: android.support.v7.widget.Toolbar
        at java.lang.Class.classForName(Native Method) 

If you get the above error message in Android Studio LogCat Logs and your App crashes, the reason is you are trying to use android.support.v7.widget.Toolbar class that does not exist, its better to use androidx.appcompat.widget.Toolbar instead.

Example:
<androidx.appcompat.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"/>
Android Toolbar ClassNotFound Exception
Android Toolbar ClassNotFound Exception


Have Questions? Post them here!





      Topics: Pages | 2015 | Tutorials | Questions | Python