I am using fragments in my dynamic application, where the User.java file contains the values and TabbedActivity.java
file contains three fragments. I want to set the text to TextView
in ProfileFrgament.java
. So, I created a TextView
in fragment_profile.xml
and referenced it from TabbedActivity.java
file with the following code
name = findViewById(R.id.name);
//getting the current user
User user = SharedPrefManager.getInstance(this).getUser();
//setting values to textviews
name.setText(user.getUsername());
It does not shows any compilation error, but after opening TabbedActivity.java
, the app stops with NullPointerException at line name.setText(user.getUsername());
How to solve this issue?
This is the code of ProfileFragment.java
file
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
name = getActivity().findViewById(R.id.name);
//getting the current user
User user = SharedPrefManager.getInstance(getActivity()).getUser();
//setting values to textviews
name.setText(user.getUsername());
return inflater.inflate(R.layout.fragment_profile, container, false);
}