NullPointerException when accessing a Fragment's textView in an Activity

六眼飞鱼酱① 提交于 2020-01-15 01:25:34

问题


It seems to be a common problem, but after hours of trying random solutions provided as answers to similar questions, my code still produces a NullPointerException when I access a Fragment's textView from an Activity.

I can include (and view) my Fragment when its textView has a predefined Text or by using setText() in onCreateView() of the Fragment class but not by using a 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 relevant 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 a RelativeLayout in my main_activity.xml, fragment_textview_message is the textView in the fragment_messagebox.xml, just to let you know, what these are.

The NullPointerException is caused in set_message_success(). Any Ideas?


回答1:


You are creating a fragment but the creation cycle is not executed. This line :

Fragment_Message fragment = new Fragment_Message();

does NOT automatically calls onCreateView, thus your text view is not initialized (is null) and when you try to set its text with the following line:

fragment.set_message_success(message_green);

you get the exception since the text view member is not initialized so either initialize the text view in a constructor or define the fragment in the desired layout.

Look here for a detailed information




回答2:


The problem apparently is the lifecycle of the fragment. When I add a fragment (done in the activity's onCreate()) and define it's textView-text in the fragment's onCreateView(), the fragment will be displayed without problems. But accessing the fragment's textView in the activity's onCreate() fails, because - I guess - the fragment is not attached yet.

So I added the fragment in the activity's onCreate() and changed the fragment's textView-text in the activity's onResume(), where (as I understand it) the fragment should be attached. Furthermore, it helps to use executePendingTransactions(). My now 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);
    }
}


来源:https://stackoverflow.com/questions/26389938/nullpointerexception-when-accessing-a-fragments-textview-in-an-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!