【问题标题】:How to access a TextView inside of a DrawerLayout [duplicate]如何访问 DrawerLayout 内的 TextView [重复]
【发布时间】:2017-05-02 22:52:59
【问题描述】:

我正在尝试为 DrawerLayout 内部的 TextView 设置一个文本,问题是,每次我尝试设置文本时都会收到 NullException,也许问题是因为我没有检查它是否是打开。我没有做任何不寻常的事情......

private TextView my_textView;

onCreate.....

Drawer class...

my_textView = (Textview)findViewByid(R.id.nav_text);

谢谢!

【问题讨论】:

  • 它在NavigationView 标头中吗?

标签: java android


【解决方案1】:

如果您想访问标题视图,请尝试如下:

  View headerView = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
        navigationView.addHeaderView(headerView);

        TextView tvUserName = (TextView) headerView.findViewById(R.id.admin_header_name_tv);

【讨论】:

    【解决方案2】:

    我假设文本在 NavigationView 标头内。

    首先,初始化您的 NavigationView 标头

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    

    接下来,初始化标题内的视图。

    TextView my_textView = (TextView) navigationView.getHeaderView(0).findViewById(R.id.nav_text);
    

    现在,使用setText 方法更改文本

    my_textView.setText("New Text");
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      您需要调用drawerLayout.findViewByid() 而不是findViewByid()。因为在drawerLayout中可以使用textview。所以改变你的代码如下

      private TextView my_textView;
      
      onCreate.....
      
      Drawer class...
      
      my_textView = (Textview)drawerLayout.findViewByid(R.id.nav_text);
      

      【讨论】:

        猜你喜欢
        • 2016-11-08
        • 2017-05-19
        • 2021-01-25
        • 1970-01-01
        • 1970-01-01
        • 2019-08-01
        • 2018-06-20
        • 1970-01-01
        • 2013-11-15
        相关资源
        最近更新 更多