Search code examples
androidxmlandroid-layoutbottomnavigationview

Bottom Navigation does not work and leads to app crash


I have tried to use Bottom Navigation on my application but it doesn't work and application keep crash. here is my main activity:

package com.example.testing;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

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

            loadFragment(new HomeFragment());
            BottomNavigationView navigation = findViewById(R.id.navigation);
            navigation.setOnNavigationItemSelectedListener(this);
    }

        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment = null;

            switch (item.getItemId()) {
                case R.id.homePage:
                    fragment = new HomeFragment();
                    break;

                case R.id.chat:
                    fragment = new ChatFragment();
                    break;
            }

            return loadFragment(fragment);
        }

        private boolean loadFragment(Fragment fragment) {
            if (fragment != null) {
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.fragment_container, fragment)
                        .commit();
                return true;
            }
            return false;
        }
    }

and here is my xml 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/menu_nav" />


</android.support.constraint.ConstraintLayout>

also, I create a two fragment for the navigation, both have the same xml and class code

the fragment xml :

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Home"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

</RelativeLayout>

the fragment class :

package com.example.testing;

public class HomeFragment extends Fragment {
    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, null);

    }
}

Also I have added this code to my build.gradle

implementation 'com.android.support:design:28.0.0'

BUT here what I got in my logcat :

    --------- beginning of crash
2019-04-05 21:55:34.150 3169-3169/com.example.testing E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.testing, PID: 3169
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testing/com.example.testing.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.support.design.widget.BottomNavigationView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.support.design.widget.BottomNavigationView
     Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.BottomNavigationView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.testing.MainActivity.onCreate(MainActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: android.view.InflateException: Couldn't resolve menu item onClick handler chat in class com.example.testing.MainActivity
        at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:254)
        at android.support.v7.view.SupportMenuInflater$MenuState.setItem(SupportMenuInflater.java:482)
        at android.support.v7.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:530)
        at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:206)
        at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:128)
        at android.support.design.widget.BottomNavigationView.inflateMenu(BottomNavigationView.java:252)
        at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:187)
2019-04-05 21:55:34.152 3169-3169/com.example.testing E/AndroidRuntime:     at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
            ... 26 more
     Caused by: java.lang.NoSuchMethodException: chat [interface android.view.MenuItem]
        at java.lang.Class.getMethod(Class.java:2068)
        at java.lang.Class.getMethod(Class.java:1690)
        at android.support.v7.view.SupportMenuInflater$InflatedOnMenuItemClickListener.<init>(SupportMenuInflater.java:250)
            ... 33 more

PLEASE HELP

HERE is the menu xml :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:id="@+id/chat"
        android:icon="@drawable/chaticon"
        android:onClick="chat"
        android:title="" />


    <item android:id="@+id/homePage"
        android:icon="@drawable/homeicon"
        android:onClick="homepage"
        android:title="" />


</menu>

Solution

  • The problem is with this line in your menu XML file -

    android:onClick="chat"
    

    This is trying to refer to a method public void chat(View) which will handle the click event of your menu item. However, there's no such method defined in your activity and the app crashes as it can't find a method which it expects to find. Same with the other menu item except it crashed before it could even reach that.

    The solution? Since you've already implemented an OnClick for the bottom navigation menu, remove the onClick attribute from the menu items.