-1

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);
}
Strooks
  • 183
  • 1
  • 10

2 Answers2

3

Your TextView is in fragment_profile.xml and you trying to find that TextView in TabbedActivity.java, so NullPointerException is occured

Put this code in ProfileFrgament.java

Change your code with this one

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) 
{ 
    final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
    name = (TextView) rootView.findViewById(R.id.name);
    User user = SharedPrefManager.getInstance(getActivity()).getUser(); 
    name.setText(user.getUsername()); 

    return rootView;
}
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • It is showing error on line `User user = SharedPrefManager.getInstance(this).getUser();` saying **getInstance() in sharedPrefManager cannot be applied to ProfileFragment** – Strooks Sep 29 '18 at 09:25
  • Replace this with getActivity() – Chirag Savsani Sep 29 '18 at 09:27
  • It is now showing `name = getActivity().findViewById(R.id.name);` as **unreachable statement** – Strooks Sep 29 '18 at 09:33
  • `unreachable statement` because you write that code after return. – Chirag Savsani Sep 29 '18 at 09:34
  • Now, no compilation error, but app still stops with logcat `java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at net.softglobe.arsoddynamic.ProfileFragment.onCreateView(ProfileFragment.java:30)` – Strooks Sep 29 '18 at 09:38
  • We can not help you unless you post necessary code here. – Chirag Savsani Sep 29 '18 at 09:41
  • I have updated the question with code of `ProfileFragment.java` – Strooks Sep 29 '18 at 09:46
0

You need to do this in the fragment's java file than in the activity's java file. Now for finding view in fragment. Replace this code :

name = findViewById(R.id.name);

to :

name = getActivity().findViewById(R.id.name);

Rest of the code can be written as it is already.

Also put this type of operation in onViewCreated method. Putting this code in onCreateView may crash the app.