Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:导航抽屉标题文本视图未以编程方式设置_Android_Android Fragments_Navigation_Navigation Drawer - Fatal编程技术网

Android:导航抽屉标题文本视图未以编程方式设置

Android:导航抽屉标题文本视图未以编程方式设置,android,android-fragments,navigation,navigation-drawer,Android,Android Fragments,Navigation,Navigation Drawer,在我的活动中,我有一个片段,因此我使用导航抽屉,在导航抽屉中,我有一个标题,带有文本视图。我想以编程方式更新它 Header.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="1

在我的活动中,我有一个片段,因此我使用导航抽屉,在导航抽屉中,我有一个标题,带有文本视图。我想以编程方式更新它

Header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="@drawable/background_material"
android:orientation="vertical"
>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name Name Name"
    android:textSize="20sp"
    android:textColor="#FFF"
    android:textStyle="bold"
    android:gravity="left"
    android:paddingBottom="4dp"
    android:id="@+id/c"
    android:paddingLeft="10dp"
    android:layout_above="@+id/email"

  />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+919999999999"
    android:id="@+id/email"
    android:gravity="left"
    android:layout_marginBottom="8dp"
    android:paddingLeft="10dp"
    android:textSize="20sp"
    android:textColor="#fff"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/username"
    android:layout_alignStart="@+id/username" />


但是获取空指针异常

您应该稍微更改代码,如下所示

  //        Set Drawer Header
            NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
            View headerView = LayoutInflater.from(this).inflate(R.layout.header, navigationView, false);
            navigationView.addHeaderView(headerView);
            TextView Name = (TextView) headerView.findViewById(R.id.name);
            Title.setText("Myname");
            TextView Number = (TextView) headerView.findViewById(R.id.number);
            Email.setText("+1234567890");

您只需要像这样引用所需的textview

TextView txtUsername = (TextView) findViewById(R.id.c);
txtUsername.setText("Name Name Name");
在导航抽屉上使用getHeaderView(0)

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

View headerView = navigationView.getHeaderView(0);

//Link Views
navUsername = (TextView) headerView.findViewById(R.id.navUsername);
//Set text here
navUsername.setText("Your text here");

请输入logcat或甚至该片段代码。请发布您的logcat错误:d要可靠地更新导航抽屉标题中的
TextView
,请在方法中使用此代码。请参阅:。
TextView txtUsername = (TextView) findViewById(R.id.c);
txtUsername.setText("Name Name Name");
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

View headerView = navigationView.getHeaderView(0);

//Link Views
navUsername = (TextView) headerView.findViewById(R.id.navUsername);
//Set text here
navUsername.setText("Your text here");