Goodnight everyone,
I have a button navigation view in my application that each button goes to a Fragment, except for the central button that I use to add and I want it to go to an Activity with a form, I have the following code:
package com.isaac.appet;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import com.isaac.appet.FinderFragment.FinderFragment;
import com.isaac.appet.HomeFragment.HomeFragment;
import com.isaac.appet.NotesFragment.NotesFragment;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNavigation = findViewById(R.id.bottomNavigation);
bottomNavigation.setOnNavigationItemSelectedListener(navListener);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container,new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.btnHome:
selectedFragment = new HomeFragment();
break;
case R.id.btnFinder:
selectedFragment = new FinderFragment();
break;
case R.id.btnNotes:
selectedFragment = new NotesFragment();
break;
case R.id.btnAdd:
startActivity(new Intent(MainActivity.this, PetsFormActivity.class));
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
return true;
}
};
}
This is my attempt to make the central button go to the Activity, and in principle it does, but when I want to go back it gives me a Null Pointer, which I indicate below:
2019-06-11 23:57:08.313 28899-28907/com.isaac.appet E/com.isaac.appe: Failed to send DDMS packet REAQ to debugger (-1 of 20): Broken pipe
2019-06-11 23:57:33.756 28899-28899/com.isaac.appet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.isaac.appet, PID: 28899
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:396)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:444)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:434)
at com.isaac.appet.MainActivity$1.onNavigationItemSelected(MainActivity.java:63)
at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:204)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:115)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25889)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6746)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
When I give it, it tells me that the line that is wrong or gives a conflict is the one that makes the Fragment transaction after the switch, but I'll stay there, to see if you can give me a hand. Thanks.
What you can do would be to create a method to load the fragments and not return null when selecting the
Item
to load your activity:Inside the case call the method and assign the fragment to load.
Example:
To launch your activity it will be:
Delete this line that is before the
return true
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();