ibrahimsn98 / SmoothBottomBar

A lightweight Android material bottom navigation bar library
MIT License
1.97k stars 255 forks source link

Menu with no icons causes application to crash #10

Closed ars27196 closed 4 years ago

ars27196 commented 5 years ago

I was populating the bottom navigation with the menu, all my items did not have an icon as I planned to add them later on. The bottom navigation kept on crashing with the main error as follows:

Error inflating class me.ibrahimsn.lib.SmoothBottomBar

After a closer look into the logcat revealed the following error:

Caused by: kotlin.KotlinNullPointerException at me.ibrahimsn.lib.BottomBarParser.getTabConfig(BottomBarParser.kt:48) at me.ibrahimsn.lib.BottomBarParser.parse(BottomBarParser.kt:23)

After a few hours of debugging and looking through your code, I figured out that the library is trying to load icon and not finding any. Please put a note in the library description or handle it with a try-catch. PS: Very Cool Library, great work. Happy Coding.

ibrahimsn98 commented 4 years ago

Now, it will throw exception if you don't set item icon. Thank you :)

ahmedkabut commented 4 years ago

Now, it will throw exception if you don't set item icon. Thank you :)

ibrahimsn i try for 1 week to how to show fragment plesss help

ars27196 commented 4 years ago

SALAM Ahmed, The SmoothBottomBar is a simple view that animated to clicked Icons. The library provides a callback that tells which item was selected. If you want to navigate to different fragments on item click you have to manage them by yourself. It is a tiring job especially if the number of fragments is greater than three. I did it for the three fragments and used Navigation Graph to help me navigate (Using fragment manager would work the same just different method calls.), Here is the XML file :

<androidx.constraintlayout.widget.ConstraintLayout                                                                                                                                                                      
   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=".view.activities.UserHomeActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/user_toolbar"
    android:background="@color/black"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

<fragment
    android:id="@+id/my_user_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@+id/userBottomBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/user_toolbar"
    app:navGraph="@navigation/user_navigation" />

<me.ibrahimsn.lib.SmoothBottomBar
    android:id="@+id/userBottomBar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    app:menu="@menu/user_menu"
    app:backgroundColor="@color/black"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

`

You can see that SmoothNavigationBar is at the bottom then there is the fragment that acts as a container for different fragments and the toolbar at the top. Now for the code part you have to explicitly set what happens when each item is clicked (For example, replacing current fragment with new fragment).

ahmedkabut commented 4 years ago

` bottomBar.setOnItemSelectedListener(object: OnItemSelectedListener {

     override fun onItemSelect(pos: Int) {

            var selectedFragment: Fragment? = null

            when (pos) {

                R.id.nav_home -> selectedFragment = HomeFragment()
                R.id.nav_music -> selectedFragment = MusicFragment()
                R.id.nav_star -> selectedFragment = StarFragment()
            }

            if (selectedFragment != null) {
                supportFragmentManager.beginTransaction().replace(R.id.fragment_container,
                    selectedFragment!!
                ).commit()
            }

            true

        }
    })

    //I added this if statement to keep the selected fragment when rotating the device
    if (savedInstanceState == null) {
        supportFragmentManager.beginTransaction().replace(R.id.fragment_container,
            HomeFragment()).commit()
    }`

and it doous nit work

ahmedkabut commented 4 years ago

m=

SALAM Ahmed, The SmoothBottomBar is a simple view that animated to clicked Icons. The library provides a callback that tells which item was selected. If you want to navigate to different fragments on item click you have to manage them by yourself. It is a tiring job especially if the number of fragments is greater than three. I did it for the three fragments and used Navigation Graph to help me navigate (Using fragment manager would work the same just different method calls.), Here is the XML file :

<androidx.constraintlayout.widget.ConstraintLayout                                                                                                                                                                      
   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=".view.activities.UserHomeActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/user_toolbar"
    android:background="@color/black"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

<fragment
    android:id="@+id/my_user_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@+id/userBottomBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/user_toolbar"
    app:navGraph="@navigation/user_navigation" />

<me.ibrahimsn.lib.SmoothBottomBar
    android:id="@+id/userBottomBar"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    app:menu="@menu/user_menu"
    app:backgroundColor="@color/black"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

`

You can see that SmoothNavigationBar is at the bottom then there is the fragment that acts as a container for different fragments and the toolbar at the top. Now for the code part you have to explicitly set what happens when each item is clicked (For example, replacing current fragment with new fragment).

ars27196

you know my job stop on this and my boss be angry if i dont know how to to that

can i take 5min from you to make it for me ples????

ahmedkabut commented 4 years ago

the god well be thank you for that ples

ars27196 commented 4 years ago

Salam, I will be happy to help you, you can get in touch with me on this email address: ars27196@gmail.com.