0

I have an activity in which upon loading I want to update a textfield. That textfield is stored in a header for that activity's layout in an external XML file. When trying to set text pointed at that XML file, I get a nullpointer exception.

Here is the activity in which I am using:

public class NavDrawerMain extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

//JAC - need to add big search box right at the top of the drawer

//stuff for the user Timeline
private CustomAdapter timelineAdapter;
private ListView timelineList;

private PullToRefreshView mPullToRefreshView;

public static final int REFRESH_DELAY = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav_drawer_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 userName = (TextView) findViewById(R.id.drawerUserName);
    userName.setText("test");

Here is the layout for that activity:

<RelativeLayout
    android:layout_width="match_parent"
    android:background="#e5e5e5"
    android:layout_height="wrap_content">

    <com.yalantis.phoenix.PullToRefreshView
        android:id="@+id/pull_to_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/timeListView"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:divider="@null"
            android:dividerHeight="0dp"/>

    </com.yalantis.phoenix.PullToRefreshView>


</RelativeLayout>

<include layout="@layout/app_bar_nav_drawer_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_nav_drawer_main"
    app:menu="@menu/activity_nav_drawer_main_drawer" />

and lastly here is the external xml file that houses the TextView that I want to change. This file is the header of the activity's xml file

<?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="@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">
android:id="+id/RelativeLayout1"      

<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/drawerUserName" />

the textviews id is drawerUserName. I had read somewhere that I need to inflate the layout but haven't been able to figure it out.

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Joe Caraccio
  • 1,899
  • 3
  • 24
  • 41

1 Answers1

0

In your activity inflate header_layout like this

View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, null);
TextView userName = (TextView) headerView.findViewById(R.id.drawerUserName);
userName.setText("test");

Now add it as a HeaderView to NavigationView

navigationView.addHeaderView(headerView);

And that should be it!

For smiliar question check; NavigationView how to handle dynamic header content

Community
  • 1
  • 1
Markus Maga
  • 377
  • 2
  • 7