R.id.icon
is already defined in Android API level 1. However, I am getting an NPE in a Android Design Support Library
internal code while accessing an imageview inflated using R.id.icon
.
NOTE: I am not targetting Oreo as my targetSdk
is currently 25
.
Here is the stacktrace:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.ImageView.getLayoutParams()' on a null object reference
at android.support.design.internal.BottomNavigationItemView.setChecked(BottomNavigationItemView.java:172)
at android.support.design.internal.BottomNavigationItemView.initialize(BottomNavigationItemView.java:97)
at android.support.design.internal.BottomNavigationMenuView.buildMenuView(BottomNavigationMenuView.java:276)
at android.support.design.internal.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:62)
at android.support.design.widget.BottomNavigationView.inflateMenu(BottomNavigationView.java:238)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:167)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:116)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.who.android.app.layout.activities.Sample_Mock.onCreate(Sample_Mock.java:40)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
On investigation, I found out that the code in BottomNavigationItemView.java is trying to access a null ImageView
inflated with R.id.icon
resource.
LayoutParams iconParams = (LayoutParams) mIcon.getLayoutParams();
where mIcon
is null
and has been inflated as
mIcon = (ImageView) findViewById(R.id.icon);
Edit: I created a new BottomNavigationActivity
from Android's Gallery and redirected the navigation to this mock class instead of mine. Still I am facing the same issue. Here are the pristine java code and the layout xml code for the new mocked activity :
package com.who.android.app.layout.activities;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import com.who.android.app.R;
public class Sample_Mock extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample__mock);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
Here is the pristine layout file where I use the BottomNavigationView (as created by Android, I have not touched it)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context="com._._._._.activities.Sample_Mock">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"/>
</LinearLayout>
The version of Android Design Support Library and Android AppCompat support libraries I am using are both 25.3.1
com.android.support:design:25.3.1
com.android.support:appcompat-v7:25.3.1
Can anyone help me in understanding why I am getting a null reference out of inflating from R.id.icon ? This code is beyond my control as it is internal to the Android Design Support library. Also, as I showed that this problem occurs in a newly created BottomNavigationActivity
as well, is there a problem with the library version I am using? Because this version has been working fine for last several months for me.
Edit 2: I created another project altogether with just one BottomNavigationActivity in it. Guess what? Same error there. So it seems to me do be an Android Design Support
Library issue.
Edit 3: menu/navigation.xml As you can see, it is standard.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home"/>
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard"/>
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications"/>
</menu>