当前位置: 首页 > 知识库问答 >
问题:

Android navigatorDrawer头中的SetText

季博
2023-03-14
 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;
 }

编辑:我的日志:

共有1个答案

马侯林
2023-03-14

删除此属性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");

这在我的案子里起作用了

 类似资料:
  • 问题内容: 我想无头运行Firefox。 Firefox不隐藏浏览器窗口或在虚拟桌面中打开它,而是通过使用“ -headless”标志来支持无头模式。 问题是我知道如何在Chrome中执行此操作,但在Firefox中却不行。 我的代码: 我的WinForm应用程序只有一个名为StartBtn的按钮。单击该按钮后,Firefox应该无头运行,但会在正常窗口中打开。 更新 我将firefox更新为56

  • 我想知道是否有人能解释一下:

  • 问题内容: 我正在使用MIMEText在Python 3.2中从头开始创建电子邮件,但在创建主题中包含非ASCII字符的消息时遇到了麻烦。 例如 最后一行给我错误 如何告诉MIMEText主题不是ascii?根本没有帮助,无论如何,我已经看到人们使用unicode字符串,而其他答案没有问题(例如,参见Python- 如何发送utf-8电子邮件? ) 编辑:我想补充一下,相同的代码在Python 2

  • 我有两个钥匙扣客户, Angular 4:具有访问类型凭据身份验证 一个JAX-RS应用程序(它将是资源服务器):具有仅承载身份验证!在这个客户端中,我们激活CORS,如下面的json所示 问题是,JAX-RS应用程序的状态代码为401(未授权)的HTTP响应没有给javascript客户端带来所需的CORS报头! 当HTTP状态为401时,我们如何添加相应的CORS标头?

  • 问题内容: 我正在使用来开发iPad2中的前置摄像头应用程序。 当我捕获图像时,它显示为从左向右翻转。 我该如何纠正? 问题答案: 您可以使用源图像翻转图像 编辑: 添加了快速代码

  • 通过执行一个简单的循环创建线程java程序,当我使用-Xmx2048M时,AIX的线程数限制在900左右,当我将该参数降低到-Xmx1024M时,线程数限制增加到2000左右。我的ulimited属性都是无限的:ulimited-a时间(秒)无限文件(块)无限数据(kbytes)无限堆栈(kbytes)无限内存(kbytes)无限核心转储(块)无限nofile(描述符)无限线程(每个进程)无限进程