I have a bottom navigation view and at the center of bottom navigation view I have a floating action button. I start fragments when click on bottom navigation view items and when click on float action button I start an activity. Fragments are loading correctly but when I click on fab button sometimes it run correctly but sometimes it gives error.
I think, it gives error because I try to load fragment in activity and then I try to open a different activity when a fragment loaded in activity. But I couldn't fix it.
Menu :
https://i.sstatic.net/V5auA.jpg
Relative Part of Main Activity :
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Context context;
private SharedPreference sharedPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
sharedPreference = SharedPreference.getInstance(context);
if (getIntent().hasExtra(Constants.SCREEN_TAG)) {
String type = getIntent().getStringExtra(Constants.SCREEN_TAG);
}
FloatingActionButton fabMainActivity = findViewById(R.id.fabMainActivity);
BottomNavigationView bnvMainActivity = findViewById(R.id.bnvMainActivity);
bnvMainActivity.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener);
loadFragment(new DiscoverFragment());
fabMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, PostJobActivity.class));
}
});
}
private BottomNavigationView.OnNavigationItemSelectedListener onNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
case R.id.navigation_discover:
selectedFragment = new DiscoverFragment();
break;
case R.id.navigation_nearby:
selectedFragment = new NearbyFragment();
break;
case R.id.empty_menu:
break;
case R.id.navigation_notification:
selectedFragment = new NotificationFragment();
break;
case R.id.navigation_profile:
selectedFragment = new ProfileFragment();
break;
}
loadFragment(selectedFragment);
return true;
}
};
private void loadFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flMainActivity, fragment);
fragmentTransaction.commit();
}
}
Main Activity XML:
<?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:layout_height="match_parent"
tools:context=".Activity.MainActivity"
android:background="@color/app_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rlTopBackgroundMainActivity"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="@drawable/top_background"
android:layout_gravity="top">
<ImageView
android:id="@+id/ivLogoWithTextMainActivity"
android:layout_width="142dp"
android:layout_height="41dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="23dp"
android:layout_marginEnd="247dp"
android:layout_marginRight="247dp"
android:layout_marginBottom="17dp"
android:layout_centerVertical="true"
android:contentDescription="@string/terms_activity_logo_with_text"
android:src="@drawable/logo_with_text" />
<ImageView
android:id="@+id/ivMessagesMainActivity"
android:layout_width="33dp"
android:layout_height="41dp"
android:src="@drawable/message_icon"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:contentDescription="@string/messages_icon" />
</RelativeLayout>
<FrameLayout
android:id="@+id/flMainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/rlTopBackgroundMainActivity" />
</RelativeLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bnvMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_gravity="bottom"
app:itemIconTint="@color/bottom_menu_color"
app:menu="@menu/bottom_navigation_menu"
app:labelVisibilityMode="unlabeled" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bapMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:itemIconTint="@color/bottom_menu_color"
app:labelVisibilityMode="unlabeled"
app:fabAlignmentMode="center"
app:fabCradleMargin="7dp"
app:elevation="0dp"
android:layout_gravity="bottom"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_2"
app:tint="@android:color/white"
android:src="@drawable/post_job_icon"
app:layout_anchor="@id/bapMainActivity"
app:maxImageSize="35dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Relative Part of Post Job Activity : (Post Job Activity also in frame layout.)
public class PostJobActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_job);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
context = PostJobActivity.this;
btnBackToMainActivityPostJobActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(PostJobActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
}
@Override
public void onBackPressed() {
Intent intent = new Intent(PostJobActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
}
Error Message:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at androidx.fragment.app.FragmentTransaction.doAddOp(FragmentTransaction.java:161)
at androidx.fragment.app.BackStackRecord.doAddOp(BackStackRecord.java:179)
at androidx.fragment.app.FragmentTransaction.replace(FragmentTransaction.java:225)
at androidx.fragment.app.FragmentTransaction.replace(FragmentTransaction.java:200)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.loadFragment(MainActivity.java:133)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity.access$000(MainActivity.java:58)
at dk.lejekspert.lejekspertrekrutter.Activity.MainActivity$3.onNavigationItemSelected(MainActivity.java:126)
at com.google.android.material.bottomnavigation.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:243)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:124)
at android.view.View.performClick(View.java:5723)
at android.view.View$PerformClick.run(View.java:22689)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6364)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Any idea to solve this problem? Thanks.
Edit : I added a sentence to my bottom_navigation_menu.xml file to make not enable menu item below fab button.
<item
android:id="@+id/empty_menu"
app:showAsAction="always"
android:enabled="false"
android:title="@string/nearby" />
Also this code part is not necessary anymore.
case R.id.empty_menu:
break;
The issue happens when you call fragmentTransaction.replace(R.id.flMainActivity, fragment);
with fragment
= null
.
Check the onNavigationItemSelected
.
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()){
//...
case R.id.empty_menu:
break; // <-- this case
}
loadFragment(selectedFragment);
return true;
In this case the selectedFragment
is null
.
Checking the screen in the question it seems that you are using in the BottomNavigationView
a menuItem without icon exactly behind the FAB.