Reputation: 328
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
Upvotes: 0
Views: 2548
Reputation: 81
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!!
Upvotes: 3
Reputation: 328
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.
Upvotes: 1
Reputation: 1633
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");
Upvotes: 0