Why do I have this error hiding this MenuItem?

1

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
    
asked by masterfabela 11.10.2016 в 10:38
source

3 answers

0

Ok, I found the solution to my problem, with a side menu I was referring to a NavigationView , I just had to use the method NavigationView.getMenu() to get the Menu where the items would be and look for them

First, Instance the NavigationView and associate it with its XML resource

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

After that I can already get the menu of items from NavigationView

Menu nav_menu = navigationView.getMenu();

After that menu of items I can extract the items according to their IDs in the XML resources

MenuItem logInItem = nav_menu.findItem(R.id.nav_manage);
MenuItem logOutItem = nav_menu.findItem(R.id.nav_closeSession);
    
answered by 14.10.2016 / 16:27
source
1

The objects in the menu should not be searched with findViewById. What you can do is re-create the menu by calling invalidateOptionsMenu (), which will cause the onCreateOptionsMenu method to run again and there you will have all the menu objects to do with them whatever you want.

    
answered by 11.10.2016 в 11:11
1

Look at one thing that I do is after popular or inflate my menu I get the items from that menu and I keep them as an attribute in my activity and then use them when I need it and it has worked great for me you can do something like this in your case .

MenuItem logInItem;
MenuItem logOutItem;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_prueba, menu);

    logInItem = (MenuItem) menu.findItem(R.id.nav_manage);
    logOutItem = (MenuItem) menu.findItem(R.id.nav_closeSession);

    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    invalidateOptionsMenu(); // Para asegurarse que lo vuelva a crear

    /** Utilizas los items como te quieras **/
    logInItem.setVisible(false);
    logOutItem.setVisible(true);
}
    
answered by 12.10.2016 в 21:13