0

I'm attempting to set the text of a TextView based on a value retrieved from Firebase databse. I am able to print the string within the method and I have added a check to not set the text if the String is null, yet the check is passed and when I call setText I get a null object reference.

Here is MapsActivity.java

private TextView headerText;  
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseUser currentUser = mAuth.getCurrentUser();
private FirebaseDatabase database = FirebaseDatabase.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);


    headerText = (TextView) findViewById(R.id.header_text);
    DatabaseReference ref = database.getReference("Users/"+currentUser.getUid()+"/user_name");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String userName = (String) dataSnapshot.getValue();

            //debugging
            Toast.makeText(MapsActivity.this, userName, Toast.LENGTH_SHORT).show();

            //passes check and attempts to set
            if(userName!=null)
                headerText.setText(userName);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here is nav_header.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="90dp"
      android:background="?attr/colorPrimary"
      android:padding="16dp"
      android:theme="@style/AppTheme"
      android:orientation="vertical"
      android:gravity="bottom">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header_text">

</TextView>

I've ensured the reference is valid and the String is being assigned to userName. I'm using onDataChange to set values in various other places in the project and haven't come across this issue before

Thanks for any help

EDIT: For completeness, here activity_maps.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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#eaeaea"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="Map" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </fragment>

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/nav_menu"
    app:headerLayout="@layout/nav_header"/>

  </android.support.v4.widget.DrawerLayout>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mackt
  • 3
  • 2

1 Answers1

0

Use this NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); View header = navigationView.getHeaderView(0) TextView headerText = (TextView) header.findViewById(R.id.header_text);

then set in the TextView like this

headerText.setText(userName);
theanilpaudel
  • 3,348
  • 8
  • 39
  • 67