skip to Main Content

I’m trying to make a basic Android App. It has its Main Activity and 4 Fragments. I’m facing an error whenever I try to implement Java Code in Main Activity for the working of my Fragments. The app has a Bottom Navigation Bar which has 4 fragments and a floating action button in between.

screenshot of main activity XML file

Logcat Error:

type here

Unable to start activity ComponentInfo{com.machinelearning.bhoomit/com.machinelearning.bhoomit.MainActivity}: java.lang.ClassCastException: com.google.android.material.bottomnavigation.BottomNavigationItemView cannot be cast to com.google.android.material.floatingactionbutton.FloatingActionButton
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)

Main Activity XML Code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:background="@color/black"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frame_layout_one" />


<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:fabAlignmentMode="center"
    android:layout_gravity="bottom"
    android:background="@color/black"
    app:fabCradleMargin="10dp"
    app:fabCradleVerticalOffset="10dp"
    app:fabCradleRoundedCornerRadius="20dp">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottomNavView"
        app:labelVisibilityMode="labeled"
        android:background="@drawable/transparent_bg"
        app:menu="@menu/bottom_nav_menu" >

    </com.google.android.material.bottomnavigation.BottomNavigationView>



</com.google.android.material.bottomappbar.BottomAppBar>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:backgroundTint="#20D45E"
        android:contentDescription="@string/app_name"
        android:src="@drawable/mic_nav"
        app:layout_anchor="@+id/bottomAppBar"
        app:layout_anchorGravity="center" />



</androidx.coordinatorlayout.widget.CoordinatorLayout>

Main Activity Java File:



package com.machinelearning.bhoomit;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import com.machinelearning.bhoomit.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {


    //working on arranging fragments
    FrameLayout frame_layout_one;
    BottomNavigationView bottomNavOne;

    ActivityMainBinding binding;

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


        //error fix wala code:
        java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());



        frame_layout_one=(FrameLayout) findViewById(R.id.frame_layout_one);
        bottomNavOne = (BottomNavigationView) findViewById(R.id.bottomNavView);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        replaceFragment(new HomeFragment());



        binding.bottomNavView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                int menuItemID = item.getItemId();

                if(menuItemID==R.id.home){
                    replaceFragment(new HomeFragment());
                    return true;
                }

                else if(menuItemID==R.id.crops){
                    replaceFragment(new CropsFragment());
                    return true;
                }

                else if(menuItemID==R.id.cam){
                    replaceFragment(new CameraFragment());
                    return true;
                }

                else if(menuItemID==R.id.profile){
                    replaceFragment(new ProfileFragment());
                    return true;
                }





                return true;
            }
        });






    }



    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frame_layout_one, fragment);
        fragmentTransaction.commit();
    }
}

Android Manifest File:

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Bhoomit"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2

Answers


  1. The FloatingActionButton should be a child of the BottomAppBar, but in your XML, it seems like it’s placed directly under the CoordinatorLayout.

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        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:background="@color/black"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/frame_layout_one" />
    
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:fabAlignmentMode="center"
            android:layout_gravity="bottom"
            android:background="@color/black"
            app:fabCradleMargin="10dp"
            app:fabCradleVerticalOffset="10dp"
            app:fabCradleRoundedCornerRadius="20dp">
    
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/bottomNavView"
                app:labelVisibilityMode="labeled"
                android:background="@drawable/transparent_bg"
                app:menu="@menu/bottom_nav_menu" />
    
            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:backgroundTint="#20D45E"
                android:contentDescription="@string/app_name"
                android:src="@drawable/mic_nav"
                app:layout_anchor="@+id/bottomAppBar"
                app:layout_anchorGravity="center" />
    
        </com.google.android.material.bottomappbar.BottomAppBar>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
    Login or Signup to reply.
  2. Try custom bottom bar using Linearlayout

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search