I'm doing an application on android where I have 2 Activities
one is the main menu and the other is a login screen, To access the login activity I have a side menu with a MenuItem
that puts Log In, after logging in and receiving the result of the Activity
I want this to hide and die another MenuItem
to loosen, but I get an error of NullPointedException
when trying to use the methods .setVisible()
so I understand that the .findViewById()
I use to associate the elements created in XML is not working, but I do not know why or how I can solve it, I attach the code fragment
OnActivityResult () code where I try to hide a MenuItem
and show the other one
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == USERNAME_REQUEST && resultCode == RESULT_OK) {
MenuItem logInItem = (MenuItem)findViewById(R.id.nav_manage);
MenuItem logOutItem = (MenuItem) findViewById(R.id.nav_closeSession);
logInItem.setVisible(false); //Error aqui
logOutItem.setVisible(true);
user = data.getStringExtra("user");
Log.d("Volviendo al Main ",user);
presentarUrls(savedState,drawer);
subtitle = (TextView) findViewById(R.id.subtitle);
subtitle.setText(user);
}
}
XML file where I define the menu items
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Settings">
<menu>
<item
android:id="@+id/nav_manage"
android:icon="@drawable/ic_menu_manage"
android:title="Login" />
<item
android:id="@+id/nav_closeSession"
android:icon="@drawable/common_full_open_on_phone"
android:title="Log Out"
android:visible="false"></item>
</menu>
</item>
<item android:title="Notifications">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_menu_share"
android:title="Notifications" />
</menu>
</item>
And the error code that Android Studio brings out
Process: es.app.appff, PID: 17667
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { cmp=es.app.appfff/.Login (has extras) }} to activity {es.app.appfff/es.app.appfff.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setVisible(boolean)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setVisible(boolean)' on a null object reference