Search code examples
javaandroidandroid-layoutandroid-viewandroid-switch

Error inflating MaterialAnimatedSwitch


I am building an app, in which I have used animated switch button by glomadrain. When I was done with my coding I got following error:

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:750) at com.github.glomadrian.materialanimatedswitch.painter.IconPainter.initBitmap(IconPainter.java:41) at com.github.glomadrian.materialanimatedswitch.painter.IconPainter.init(IconPainter.java:36) at com.github.glomadrian.materialanimatedswitch.painter.IconPainter.(IconPainter.java:30) 11-25 14:05:04.032 4625-4625/com.example.surajnath.taproute E/AndroidRuntime: at com.github.glomadrian.materialanimatedswitch.painter.IconPressPainter.(IconPressPainter.java:30) at com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch.initPainters(MaterialAnimatedSwitch.java:79) at com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch.init(MaterialAnimatedSwitch.java:65) at com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch.init(MaterialAnimatedSwitch.java:90) at com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch.(MaterialAnimatedSwitch.java:54)

Here my code regarding to the map and the switch :

<com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch
    android:layout_width="0dp"
    android:layout_weight="3"
    android:id="@+id/location_switch"
    android:layout_height="wrap_content"
    app:icon_release="@drawable/ic_location_off_black_24dp"
    app:icon_press="@drawable/ic_location_on_black_24dp"
    app:base_release_color="@color/baseReleaseColor"
    app:base_press_color="@color/basePressColor"
    app:ball_release_color="@color/ballReleaseColor"
    app:ball_press_color="@android:color/white"/>

Solution

  • The problem is, that you are using a vector drawable as an input to app:icon_press and app:icon_release, whereas the library does not support vector drawable.

    You have to either fork the library and perform necessary changes when creating a Bitmap, or you can provide a non-vector drawable as an input to those parameters.