NullPointerException when accessing a Text Fragment texture in action

This seems to be a common problem, but after hours of trying random solutions provided as answers to similar questions, my code still throws a NullPointerException when I access the text element of a fragment from an Activity.

I can include (and view) my fragment when its textView has predefined text, or using setText()

in onCreateView()

the Fragment class, but not using the setter method (here set_message_success()

).

This is my Fragment class Fragment_Message

:

public class Fragment_Message extends Fragment {

    TextView textview_message;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_messagebox, container, false);
        textview_message = (TextView) view.findViewById(R.id.fragment_textview_message);
        textview_message.setText("test");
        return view;
    }

    public void set_message_success(String text) {
        textview_message.setText(text);
    }
}

      

And the corresponding lines from the Activity class:

FragmentManager frag_manager    = getFragmentManager();
FragmentTransaction transaction = frag_manager.beginTransaction();
Fragment_Message fragment       = new Fragment_Message();
fragment.set_message_success(message_green);
transaction.add(R.id.home_fragment_container, fragment, "Fragment");
transaction.commit();

      

home_fragment_container

is the RelativeLayout in my main_activity.xml, fragment_textview_message

is the textView in the fragment_messagebox.xml file, just so you know what it is.

A NullPointerException was thrown in set_message_success()

. Any ideas?

+3


source to share


2 answers


You create a snippet, but the build loop fails. This line:

Fragment_Message fragment = new Fragment_Message();

      

DOES NOT automatically call onCreateView

, so your text view is not initialized (this null

) and when you try to set its text with the following line:



fragment.set_message_success(message_green);

      

you get an exception because the text viewer is not initialized to either initialize the text view in the constructor or define a snippet in the desired layout.

Look here for details

+1


source


The problem appears to be with the fragment lifecycle. When I add a fragment (done in action onCreate()

) and define its textView text in the fragment onCreateView()

, the fragment will display without issue. But accessing the fragment of the textView in the activity onCreate()

fails because - I guess the fragment hasn't been attached yet.

So, I added a fragment to the activities onCreate()

and changed the textView-text fragment in the activity onResume()

where (as I understand it) the fragment should be attached. It also helps to use executePendingTransactions()

. My working code:

Home.java



FragmentManager     frag_manager;
FragmentTransaction transaction;
Fragment_Message    fragment;


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

    frag_manager    = getFragmentManager();
    transaction     = frag_manager.beginTransaction();
    fragment        = new Fragment_Message();

    transaction.add(R.id.home_fragment_container, fragment, "Fragment_Messagebox");
    transaction.commit();
}


@Override
protected void onResume() {
        frag_manager.executePendingTransactions();          
        fragment.set_message_success("a message");
}

      

Fragment_Message.java

public class Fragment_Message extends Fragment {

    TextView textview_message;
    View view;

    public Fragment_Message() {}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_messagebox, container, false);
        return view;
    }


    public void set_message_success(String text) {
        textview_message = (TextView) view.findViewById(R.id.fragment_textview_message);
        textview_message.setText(text);
    }
}

      

0


source







All Articles