在活动访问片段的TextView的NullPointerException异常时 [英] NullPointerException when accessing a Fragment's textView in an Activity

查看:277
本文介绍了在活动访问片段的TextView的NullPointerException异常时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个常见的​​问题,而是试图为回答类似的问题提供解决方案,随机小时后,我的code仍然会产生一个NullPointerException异常,当我从一个活动访问片段的TextView的。

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.

我可以包括(和视图)我的片段时,其TextView中有predefined文本或使用的setText() onCreateView( )不使用setter方法​​(这里 set_message_success()的片段类,但)。

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()).

这是我的片段类 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 是一个RelativeLayout的在我的main_activity.xml, fragment_textview_message 是fragment_messagebox.xml TextView的,只是让你知道,这是什么意思。

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.

该NullPointerException异常是引起set_message_success()。任何想法?

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

推荐答案

您正在创建一个片段,但制作周期不执行。
该行:

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

Fragment_Message fragment = new Fragment_Message();

不自动调用 onCreateView ,因此你的文本视图未初始化(为),当您尝试设置其文本与以下行:

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.

这里的详细信息。

这篇关于在活动访问片段的TextView的NullPointerException异常时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆