Dexter Brian
Dexter Brian

Reputation: 41

binary xml file line error inflating bottom navigation view on lower android version

I'm a beginner in android programming and I've been trying to implement the bottom navigation view in my app to work on API 17 and above but on running the app on my API 22 Lollipop phone the app crashes and it also crashes on all other android versions lower than API 24 (android 7.0).

Here is the error as it appears on the logcat:

01-30 02:09:40.573 2435-2435/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dexter.myapp/com.dexter.myapp.activities.Myapp}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.BottomNavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.BottomNavigationView
        at android.view.LayoutInflater.createView(LayoutInflater.java:613)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
        at com.dexter.myapp.activities.Myapp.onCreate(Myapp.java:43)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
        at android.app.ActivityThread.access$600(ActivityThread.java:141) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5041) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
        at android.view.LayoutInflater.createView(LayoutInflater.java:587)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
        at com.dexter.myapp.activities.Myapp.onCreate(Myapp.java:43) 
        at android.app.Activity.performCreate(Activity.java:5104) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
        at android.app.ActivityThread.access$600(ActivityThread.java:141) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5041) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
        at dalvik.system.NativeStart.main(Native Method) 
     Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070064
        at android.content.res.Resources.getValue(Resources.java:1014)
        at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:330)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
        at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
        at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
        at android.support.v7.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:491)
        at android.support.design.internal.BottomNavigationItemView.initialize(BottomNavigationItemView.java:100)
        at android.support.design.internal.BottomNavigationMenuView.buildMenuView(BottomNavigationMenuView.java:281)
        at android.support.design.internal.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:62)
        at android.support.design.widget.BottomNavigationView.inflateMenu(BottomNavigationView.java:236)
        at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:165)
        at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
        at java.lang.reflect.Constructor.constructNative(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
        at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
        at com.dexter.myapp.activities.Myapp.onCreate(Myapp.java:43) 
        at android.app.Activity.performCreate(Activity.java:5104) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
        at android.app.ActivityThread.access$600(ActivityThread.java:141) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:5041) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
        at dalvik.system.NativeStart.main(Native Method)

Here is the Myapp.java file line 43:

setContentView(R.layout.my_app_layout);

Here is my_app_layout:

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.Myapp">

<FrameLayout
       android:id="@+id/fragment_container"
       android:layout_width="match_parent"
       android:layout_above="@+id/navigation"
       android:layout_height="match_parent">
</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/navigation"
    app:itemIconTint="#000000"
    app:itemTextColor="#000000" />

</RelativeLayout>

I hope that's enough to find what the problem might be and fix it.

Please note that the app used to work on API 22 so I have been wondering whether downloading the new Android Studio 3.3. Here's more about the android version I am currently using:

 Android Studio 3.3
 Build #AI-182.5107.16.33.5199772, built on December 25, 2018
 JRE: 1.8.0_152-release-1248-b01 amd64
 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
 Windows 10 10.0

Thanks in advance.

Upvotes: 2

Views: 2094

Answers (3)

soulflavacrew
soulflavacrew

Reputation: 2836

In my case, the problem was in the usage of the non-material components parent theme for the activity that contained bottom navigation view. Changing the style from

<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">

to

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

fixed the issue for me.

Upvotes: 0

Sarath SVS
Sarath SVS

Reputation: 49

I got the java.lang.reflect.InvocationTargetException too, becuase i tried to add more than 5 items to the BottomNavigationView.

And I solve it by removing some item from the menu, then the error is gone.

Upvotes: 0

Lucem
Lucem

Reputation: 3082

Welcome to Stack Overflow.

The log indicates 2 errors when running your application.

1.java.lang.reflect.InvocationTargetException Here you can use the annotation targetapi

2.android.content.res.Resources$NotFoundException: Resource ID #0x7f070064 Here you are using a resource that does not exist.

When you face such a layout problem in Android, an easy debugging tip is to remove all customizations of the view to know where the problem is coming from. In your view, for instance, attribute app was added on api level 21. Also, you need to confirm if these resources are available before using them ?android:attr/windowBackground

Here is a working example

<android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="8dp"
        android:focusable="true"
        android:focusableInTouchMode="true"
        app:menu="@menu/navigation"
        tools:targetApi="lollipop" />

Notice that the targetApi limits lower apis

Upvotes: 1

Related Questions