When I pass a variable from the activity of the navigation drawer to a fragment, it gives me an error of null when retrieving that variable. I'm going to put only the necessary code. This activity receives variables from the main and puts them in a bundle to send them to the fragment. This is the onCreate of the Navigation Drawer activity:
public class NavigationDrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, InicioFragment.OnFragmentInteractionListener, PerfilFragment.OnFragmentInteractionListener {
Alumno alum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
Fragment fragment = new InicioFragment();
getSupportFragmentManager().beginTransaction().add(R.id.content_drawer, fragment).commit();
String tipo = getIntent().getExtras().getString("Tipo");
Fragment perfil = new PerfilFragment();
Bundle bundle = new Bundle();
alum = (Alumno) getIntent().getExtras().getSerializable("User");
try {
Picasso.with(getApplicationContext()).load(alum.getFoto_perfil()).error(R.drawable.user).into(miImageViewDrawerFoto);
}catch(IllegalArgumentException e){
miImageViewDrawerFoto.setImageResource(R.drawable.user);
}
bundle.putSerializable("User", alum);
bundle.putString("Tipo", tipo);
perfil.setArguments(bundle);
}
}
And this is the fragment onCreate method where I retrieve the variable. The null error gives me on the String line type = getArguments (). GetString ("Type");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tipo = getArguments().getString("Tipo");
}
Thanks.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_margin="0dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.pepe.proy.Fragments.PerfilFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dni"
style="@style/LabelsMedium"
/>
</FrameLayout>