0

Basically i need to call a method, which refreshes the username in my NavigationBar. I try to call it from another activity SettingsActivity.java, where the user changes his name.

SettingsActivity.java:

// ...
MainActivity tempActivity = new MainActivity();
tempActivity.refreshNBName();
// ...

When i do this i get this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

I've tried to do this another way:

((MainActivity)getApplicationContext()).refreshNBName();

But this throws another exception:

java.lang.ClassCastException: com.justnothing.nisser.debbie.GlobalVariables cannot be cast to com.justnothing.nisser.debbie.MainActivity

The method that i'm trying to call here looks like this:

public void refreshNBName(){
    NavigationView nV = (NavigationView) findViewById(R.id.nav_view);
    View headerView = nV.getHeaderView(0);
    TextView local_user = (TextView) headerView.findViewById(R.id.actualUser);

    local_user.setText(((GlobalVariables) getApplication()).name + " " + ((GlobalVariables) getApplication()).surname);
}

What should i do here? Any help or advice is appreciated!

nisser
  • 51
  • 5
  • 1
    Possible duplicate of [Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference](https://stackoverflow.com/questions/36666091/attempt-to-invoke-virtual-method-android-view-windowcallback-android-view-wind) – Jaydip Kalkani Apr 12 '18 at 16:29
  • Activities are particular object that are not supposed to be instatiate unsing new. So your tempActivty object is not correctly instantiate and will surely contain field not initialized. So it failed – vincrichaud Apr 12 '18 at 16:31
  • So what can I do here to fix the problem? Any suggestions? – nisser Apr 12 '18 at 16:38
  • Why don't you store the username in shared preferences or pass username when you start main activity using putExtra() – Brainnovo Apr 12 '18 at 16:44
  • The problem is not that i cannot get the username, it is that i need to update the shown name in my NavBar using `refreshNBName()` – nisser Apr 12 '18 at 17:03
  • See updated answer dude. – Khemraj Sharma Apr 12 '18 at 17:54

2 Answers2

0

You wrote this which is not applicable.

MainActivity tempActivity = new MainActivity();
tempActivity.refreshNBName();

Because this is creating new instance of MainActivity class. not referring to the live instance of MainActivity.

You tried another thing, you can't even do this.

((MainActivity)getApplicationContext()).refreshNBName();

Because getApplicationContext will return you context of application class which is not MainActivity.

I have gone through your question, you need not call any method from another activity. If you want update navigation drawer. You can call refreshNBName() on onResume() of MainActivity.class. So every time user comes back to MainActivity, navigation view will update automatically.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
-1

You can create static method in MainActivity and than call the method like this MainActivity.someMethod() but i don't recommend this ! it can lead to memory leak and lots of exception to handle

if your main activity is not alive and is in the pause,stop,destory state there is no point to refreshing the view in main activity and you can always refresh the view to latest data when activity state changed to resume by overwriting onResume method in activity .

and finally i think best way to communicating with activates and fragment is using callback for more information see this link :

https://developer.android.com/training/basics/fragments/communicating.html

Amir Hossein Mirzaei
  • 2,325
  • 1
  • 9
  • 17
  • Unfortunately i cannot make the method static: "Non-static method 'findViewById(int)' cannot be referenced from a static context" – nisser Apr 12 '18 at 17:00