I'm trying to change the text of a NavigationView's child. I am using findViewById to get directly to the TextView. It works in the MainActivity, but in every other Activity findViewById returns a null reference.
By the way this is my first submitted question and I hope I respected the guidelines.
I can't seem to figure this out so I don't know where to start.
This is the NavigationView located in the layout of every activity
<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:visibility="visible"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"/>
nav_header_main is the component that has the said TextView, it is referenced in the code above. This is nav_header_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_header"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<TextView
android:id="@+id/nav_header_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Angheluta Filip"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/white" />
</LinearLayout>
This code is from MainActivity, where finding the said text view doesn't return a null reference
NavigationManager navigationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Downloads the database and than proceeds to setup the Activity
//onDataChange gets called when the download is finished and dataSnapshot is the downloaded
//data
final AppCompatActivity context = this;
FirebaseHandler.getFirebaseHandler().getReference().addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
FirebaseHandler.getFirebaseHandler().setData(dataSnapshot);
DataSnapshot userSnapshot = dataSnapshot.child("Users").child(FirebaseHandler.getFirebaseHandler().getAuth().getUid());
User.initializeCurrentUser(userSnapshot);
//Add side bar to this activity
navigationManager = new NavigationManager(context);
navigationManager.createNavBar();
//Recycler view setup
RecyclerManager recyclerManager = new RecyclerManager();
recyclerManager.createRecyclerWithLargeElements(context, Voluntariat.getDataSet());
//Sort spinner setup
SortSpinnerManager sortSpinnerManager = new SortSpinnerManager();
sortSpinnerManager.createSortSpinnerManager(context, recyclerManager);
//Search bar setup
SearchBarManager searchBarManager = new SearchBarManager();
searchBarManager.createSearchBar(context, recyclerManager, Voluntariat.getDataSet());
TextView textViewHeader = findViewById(R.id.nav_header_textView);
Log.d("textHeader", textViewHeader.getText().toString());
textViewHeader.setText(User.currentUser.lastName + " " + User.currentUser.firstName);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
But this is from any other Activity in the onCreate method
RecyclerManager recyclerManager;
NavigationManager navigationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toate_vol);
//Add side bar to this activity
navigationManager = new NavigationManager(this);
navigationManager.createNavBar();
//RECYCLERVIEW SETUP
recyclerManager = new RecyclerManager();
recyclerManager.createRecyclerWithSmallElements(this, Voluntariat.getDataSet());
//Search bar managing
SearchBarManager searchBarManager = new SearchBarManager();
searchBarManager.createSearchBar(this, recyclerManager, Voluntariat.getDataSet());
TextView textViewHeader = findViewById(R.id.nav_header_textView);
Log.d("textHeader", textViewHeader.getText().toString());
textViewHeader.setText(User.currentUser.lastName + " " + User.currentUser.firstName);
}
When I access any activity other than the MainActivity I get NullPointerException on the textViewHeader.
EDIT: I added more code and i changed it so the findViewById is called straight from the Activity file rather than from the NavigationManager file. The error is the same.
EDIT2: Added some more code from activity_toate_vol.xml:
It seems like this layout doesn't have any view with the problematic id, but the id is located in the nav_header_main.xml file which is set in the activity_toate_vol.xml file with the headerLayout attribute:
app:headerLayout="@layout/nav_header_main"
This NavigationView is a direct child of the root layout and the rest of the code is irrelevant, This same navigation view is found in the MainActivity as well.
<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:visibility="visible"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal">
<ImageView
android:id="@+id/imgdeconctare"
android:layout_width="27dp"
android:layout_height="27dp"
android:alpha=".6"
android:src="@drawable/ic_logout"
app:srcCompat="@drawable/ic_logout"
tools:srcCompat="@drawable/ic_logout" />
<TextView
android:id="@+id/logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:gravity="center|start"
android:text="Deconectare"
android:textColor="@android:color/black" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.NavigationView>
EDIT 3: I came out with a workaround and also identified the problem. Turns out I have to somehow wait for that NavigationView to inflate before I update it's text. Can anyone help me with the proper way to do this? Cause I am not happy with this.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
wait(150);
}
catch(Exception e){
}
TextView textViewHeader = findViewById(R.id.nav_header_textView);
Log.d("textHeader", textViewHeader.getText().toString());
textViewHeader.setText(User.currentUser.lastName + " " + User.currentUser.firstName);
}
}, 100);