我到处寻找,只有很多人提出这个问题,到目前为止一切都没有。我目前正在开发一个基于片段的应用程序,当有人输入他们的ID时,它会下载他们的名字和图片。我希望能够在导航标题字段中更改其名称。目前这是我正在使用的代码
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");
我从这里获得https://code.google.com/p/android/issues/detail?id=190786
我也在这里试过这个方法In android how to set navigation drawer header image and name programmatically in class file?
这两个最终都会出现此错误
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)
有谁知道如何更改导航标题侧栏的文字和图像? https://gyazo.com/23534130df4aff888708415b368aa1fa
答案 0 :(得分:1)
如果有人在将来发现这一点,我能够找到一个完全符合我需要它的库。你可以找到它here。
答案 1 :(得分:1)
NavigationView navigationView = findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
test = (TextView) header.findViewById(R.id.username);
test.setText("HELLO");
也许这会有所帮助!!
答案 2 :(得分:0)
根据您的错误,看起来navigationView为null。检查以确保在向标题膨胀之前正确初始化了导航视图。根据您使用片段的方式/位置,片段生命周期可能会在标头膨胀之后初始化navigationView,这将导致NullPointerException。
如果不是上述情况,导航标题通胀对于最近设计更改谷歌设计库时使用的android环境变得非常敏感。下面是我的全包解决方案,也许它适合你。
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");