gpt4 book ai didi

java - Android navigatorDrawer header 中的 SetText TextView

转载 作者:行者123 更新时间:2023-12-01 10:28:02 24 4
gpt4 key购买 nike

如何从导航器抽屉标题中获取textView?我正在尝试这个:

 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
TextView text = (TextView) drawer.findViewById(R.id.textViewNames);
text.setText("HELLO");

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
try {
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_profiler, null);
navigationView.addHeaderView(header);
TextView text = (TextView) header.findViewById(R.id.textViewNames);
text.setText("HELLO");
} catch (Exception e) {
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
}

但没有任何作用。我总是得到“NullPointerException”。由于某种原因,此方法只是吸引控件工作:

 @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
TextView textView = (TextView) findViewById(R.id.textViewNames);
textView.setText(LoginActivity.FIRST_NAME + "\n" + LoginActivity.LAST_NAME);

// Handle navigation view item clicks here.
int id = item.getItemId();

if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.Find) {

} else if (id == R.id.LogOut) {

}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

编辑:我的日志:

02-07 08:00:04.307 8346-8346/com.example.krystian.beacon E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.krystian.beacon, PID: 8346 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.krystian.beacon/com.example.krystian.beacon.Witaj_Activity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.krystian.beacon.Witaj_Activity.onCreate(Witaj_Activity.java:45) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5001)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)  at dalvik.system.NativeStart.main(Native Method) 

最佳答案

去掉这个属性app:headerLayout="@layout/nav_header_main"

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_activiy_drawer" />

并在MainActivity中动态添加

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.inflateHeaderView(R.layout.nav_header_main_activiy);
ImageView user_iv = (ImageView) headerView.findViewById(R.id.user_iv);
TextView fullName_tv = (TextView) headerView.findViewById(R.id.fullName_tv);
TextView userEmail_tv = (TextView) headerView.findViewById(R.id.userEmail_tv);

fullName_tv.setText("Your Full Name");
userEmail_tv.setText("Your Email id");

它在我的情况下有效

关于java - Android navigatorDrawer header 中的 SetText TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com