0

I have looked everywhere and there are only so many people that have asked this question and so far nothing is working. I am currently working on an app that is based on fragments and when someone enters their id, it downloads their name and picture. I want to be able to change their name in the nav header field. Currently this is the code I am using

View header = LayoutInflater.from(getActivity()).inflate(R.layout.nav_header_main, null);
        navigationView.addHeaderView(header);
        test = (TextView) header.findViewById(R.id.username);
        test.setText("HELLO");

That I obtained from here https://code.google.com/p/android/issues/detail?id=190786

I also tried this method here In android how to set navigation drawer header image and name programmatically in class file?

Both of these end up with this error

FATAL EXCEPTION: main

Process: com.horizonservers.horizon, PID: 4042
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.horizonservers.horizon/com.horizonservers.horizon.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.NavigationView.addHeaderView(android.view.View)' on a null object reference                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
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 virtual method 'void'
android.support.design.widget.NavigationView.addHeaderView(android.view.View)' on a null object reference
at com.horizonservers.horizon.MainFragment.onCreateView(MainFragment.java:153)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
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) 

Does anyone know how to change the Text and image of the nav header side bar? https://gyazo.com/23534130df4aff888708415b368aa1fa

Community
  • 1
  • 1
Destry
  • 228
  • 1
  • 12
  • did u find ur answer....i m also looking for answer – Manish Sep 26 '16 at 06:59
  • As far as I have seen so far there is no way of doing this. I contacted the dev of another app who's side bar does exactly that but he said he built his from scratch. – Destry Oct 14 '16 at 04:06
  • any other way to doing this.. – Manish Oct 14 '16 at 04:53
  • The way I did this was by having the user login the first time. It would take their name and whatever else I wanted in the sidebar then save it to a text file. It restarted on its own and took the strings from the text file. Sort of ugly but it works at least temporarily ... – Destry Oct 17 '16 at 13:32

3 Answers3

2
        NavigationView navigationView = findViewById(R.id.nav_view);
        View header = navigationView.getHeaderView(0);
        test = (TextView) header.findViewById(R.id.username);
        test.setText("HELLO");

Maybe this would help!!

1

If anyone finds this in the future, I was able to find a library that does pretty much exactly what I needed it to do. You can find it here.

Destry
  • 228
  • 1
  • 12
0

Based on your error, it looks like navigationView is null. Check to make sure navigationView was properly initialized BEFORE you inflate the header. Depending on how/where you're using fragments, the fragment lifecycle could be initializing the navigationView after the header inflation which will throw a NullPointerException.

If not the above, navigation header inflation has become very sensitive to the android environment used with recent design changes to the google design library. Below was my all-inclusive solution, and maybe it will work for you.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
test = (TextView) header.findViewById(R.id.username);
test.setText("HELLO");
Ryan Pierce
  • 1,583
  • 16
  • 24
  • Sorry this didn't help. I had found this answer earlier and decided to try it but I still get the same error. – Destry Aug 17 '16 at 23:47