-1

I am new to Android.

I have read through the threads and tried various solutions but none worked for me. Here is what I have which works.

public class Tab1 extends Fragment {

TextView mTextView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1, container, false);

    mTextView = (TextView)rootView.findViewById(R.id.myTextView1);
    mTextView.setText("This works"); //This works fine
    return rootView;
}

}

This will update the TextView just fine with the words "This works".

But when I try this to make it more flexible so I can update it while the app is running, it fails on me with this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.schoolproject.tabs.Tab1.changeText(Tab1.java:29)

public class Tab1 extends Fragment {

TextView mTextView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab1, container, false);

    mTextView = (TextView)rootView.findViewById(R.id.myTextView1);
    return rootView;
}

public void changeText(String mText) {
    mTextView.setText(mText);
}

}

It DOESN'T even let me compile. If I comment out the line mTextView.setText(mText) then it will compile. Why is .setText() triggering this error? My goal was to update the TextView from my MainActivity like so:

Tab1 tab1 = new Tab1();
tab1.changeText("Please Update This Text");

Thanks!

  • There are two possibilities. Either you are calling `changeText()` before `onCreateView()` is called, or else your layout doesn't have a view with id `tabby1`. A stack trace would help. – Ted Hopp Mar 14 '18 at 21:10
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Mar 14 '18 at 21:11
  • @tedhopp Sorry. Both of my textviews are called myTextView1. It doesn't let me compile even because of the changeText() error – user7655804 Mar 14 '18 at 21:18
  • If it doesn't compile, how could you possibly be getting a NPE? – Ted Hopp Mar 14 '18 at 23:55

1 Answers1

0

onCreate of a Fragment is a lifecycle event and will not be executed when you instantiate your Tab1 with new.

You have to wait until it's created to call changeText.

By:

Give the String as an argument (Bundle and setArguments) so you can get at onCreateView (getArguments().getString(KEY))

OR:

Store it as a variable in changeText and set at onCreate. This approach is a bad pratice since it cannot be reconstructed if needed, loosing it value.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • would you be able to give me some examples. i should mention it doesnt let me compile even due to this line: public void changeText(String mText) { mTextView.setText(mText); } – user7655804 Mar 14 '18 at 21:20