bimsina
bimsina

Reputation: 444

BottomNavigationViewEx Null pointer exception

My xml file contains the code:

<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:id="@+id/bnve"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemIconTint="@drawable/bottom_navigation_colors"
        app:itemTextColor="@drawable/bottom_navigation_colors"
        app:menu="@menu/bottom_nav_menu" />

My java file contains:

BottomNavigationViewEx bnve = (BottomNavigationViewEx)findViewById(R.id.bnve);

It runs perfectly on an android device. But when I add the lines:

    bnve.enableAnimation(false);
    bnve.enableShiftingMode(false);
    bnve.enableItemShiftingMode(false);

the app crashes with the error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bimsina.projectchautari/com.bimsina.projectchautari.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2583) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5765) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference at com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.getBottomNavigationItemViews(BottomNavigationViewEx.java:569) at com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.enableAnimation(BottomNavigationViewEx.java:341) at com.bimsina.projectchautari.MainActivity.onCreate(MainActivity.java:94) at android.app.Activity.performCreate(Activity.java:6331) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)

Does someone have a solution?

Edit: Heres my whole xml and java file.

  <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:id="@+id/bnve"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_menu"/>

</android.support.constraint.ConstraintLayout>

The java file is:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationViewEx bnve= findViewById(R.id.bnve);
        bnve.enableAnimation(false);
        bnve.enableShiftingMode(false);
        bnve.enableItemShiftingMode(false);
    }
}

Upvotes: 2

Views: 2317

Answers (2)

Junaid Ahmed
Junaid Ahmed

Reputation: 144

This error comes from using different version of support library that is not compatible with the library. hence for old version support lib 25 or 26 use com.github.ittianyu:BottomNavigationViewEx:1.2.4

and for new com.android.support:design:28.0.0 use com.github.ittianyu:BottomNavigationViewEx:2.0.2

This will solve the problem

Upvotes: 5

bimsina
bimsina

Reputation: 444

I found a work around for the problem.I just wanted to remove the animation. Instead of using BottomNavigationViewEx,I used the regular BottomNavigationView and added the following line which removed the animation.

app:labelVisibilityMode="unlabeled"

It will remove the label and also disable the animation.

Upvotes: 1

Related Questions