【问题标题】:Android Api 23 Change Navigation View headerLayout textviewAndroid Api 23 更改导航视图 headerLayout textview
【发布时间】:2015-10-18 15:31:36
【问题描述】:

我正在 android 中测试 Navigation Drawer 示例项目,但在导航视图配置文件标题中设置文本时遇到问题。

这是我的代码:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

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

    TextView text = (TextView) findViewById(R.id.textView);
    texto.setText("HELLO");
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">

<include layout="@layout/app_bar_main" android:layout_width="match_parent"
    android:layout_height="match_parent" />

<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:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
android:gravity="bottom">

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" />

<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="android.studio@android.com" android:id="@+id/textView" />

</LinearLayout>

当我尝试设置 textview 的文本时,我总是收到此错误,并且仅在 api 级别 23 时发生:

java.lang.NullPointerException: Attempt to invoke virtual method   'void android.widget.TextView.setText(java.lang.CharSequence)' on a null   object reference

如何从主要活动中更改 nav_header_main.xml 的 Textview 文本??

提前致谢

【问题讨论】:

  • 有同样的问题。也许图书馆有问题?
  • 我也有这个问题 - 引入了 com.android.support:* 23.1.0 中的依赖项。恢复到 23.0.1 修复了它,所以我会一直坚持到它解决为止。
  • 这里有同样的问题...我认为它是新com.android.support:* 23.1.0....中的一个错误...
  • 这确实是设计支持库 23.1.0 的问题。因为 NavigationView 的底层实现现在是 RecyclerView,所以标题布局在运行时不会膨胀。请参阅此处的链接:stackoverflow.com/questions/33364276/…
  • 另外一点:这种事情让我对自动驾驶汽车的未来不寒而栗!

标签: android nullpointerexception header textview navigation-drawer


【解决方案1】:

现在有了 23.1.1 版本的设计支持库,您可以使用

View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);

【讨论】:

  • 你也可以使用navigationView.getHeaderCount(),以防你有超过1个标题。
  • 这是支持库 23.1.1 的正确答案。
  • 为我工作:support-v4:24.1.1
【解决方案2】:

我遇到了同样的问题,并且能够使用此代码避免它:

    View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
    navigationView.addHeaderView(header);
    TextView text = (TextView) header.findViewById(R.id.textView);
    text.setText("HELLO");

【讨论】:

  • 很好的答案。完美运行
  • 如果这是唯一的解决方案,为什么我们有一个 xmls?没有办法通过xml本身进行更改吗?
  • 这工作正常,删除 `app:headerLayout="@layout/nav_header_main"` 以避免重复标题。
  • LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);,AndroidStudio 向Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element) 投诉。有没有办法解决这个问题?
  • 我在部署调试 apk 时仍然遇到同样的崩溃,但是,此后每次启动应用程序都可以正常运行...
【解决方案3】:

Orium 答案有效,这也有效。

View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
    TextView text = (TextView) header.findViewById(R.id.textView);
    texto.setText("HELLO");

【讨论】:

    【解决方案4】:

    这解决了我的问题

        View headerView = navigationView.inflateHeaderView(R.layout.header_view);
    
        txt_fullname = (TextView) headerView.findViewById(R.id.txt_fullname);
        txt_email = (TextView) headerView.findViewById(R.id.txt_email);
    

    【讨论】:

      【解决方案5】:
      View headerView=navigationView.getChildAt(0);
      TextView tvName=(TextView)hView.findViewById(R.id.name);
      TextView tvEmail=(TextView)hView.findViewById(R.id.email);
      
          tvName.setText(data);
          tvEmail.setText(data);
      

      【讨论】:

      • 此解决方案有效。我也试过:textView = navigationView.getHeaderView(0).findViewById(R.id.textview);View header = LayoutInflater.from(this).inflate(R.layout.menu_header, null); navigationView.addHeaderView(header); textView = header.findViewById(R.id.textview); 但不幸的是没有成功。我不知道为什么。
      【解决方案6】:

      或者如果你使用这个属性:

      <android.support.desibn.widget.NavigationView>
          ...
          app:headerLayout="@layout/your_header_layout"/>
      

      你可以:

      View header = mNavigationView.getHeaderView(0);
      TextView title = (TextView) header.findViewById(R.id.your_title_id);
      //Or something else
      

      【讨论】:

        【解决方案7】:

        上面的答案是对的,但是在kotlin中,你需要使用findViewById&lt;View&gt;

           val navigationHeader = navigation_view.getHeaderView(0)
                val navigationTitle = navigationHeader.findViewById<View>(R.id.txt_navigation_title) as TextView
                navigationTitle.text = "This is your header"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-02
          • 2017-06-09
          • 1970-01-01
          相关资源
          最近更新 更多