简体   繁体   English

从活动向片段发送数据时获取NullPointerException

[英]Getting NullPointerException while sending data from activity to fragment

I have implemented the fragment in android. 我已经在android中实现了该片段。 Now we try to send the data from activity to the fragment. 现在,我们尝试将数据从活动发送到片段。 But while sending the data we are getting the NullPointerException in *onCreateView* method of fragment. 但是,在发送数据时,我们在片段的*onCreateView*方法中获取了NullPointerException

Following code of Activity. 以下活动代码。

 setContentView(R.layout.fragment_1);
        FragmentManager fm = getFragmentManager();
        FragmentTranstction ft = fm.beginTransaction();
        Fragment1 f1 = new Fragment1();
                    String text1 = "prakash";
        Bundle bundle = new Bundle();
        bundle.putString("sessionName", text1);
        f1.setArguments(bundle);
        ft.add(R.id.frag1, f1);
        ft.commit();

Following code of Fragment: 以下片段代码:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = this.getArguments().getString("sessionName");    
    TextView sessionTitle = (TextView) getView()
            .findViewById(R.id.session1);
    sessionTitle.setText(strtext);
    return inflater.inflate(R.layout.fragment_1, container, false);
}

what is the mistake i did, because of that i am getting NullPointerException , please help 我犯了什么错误,因此我得到了NullPointerException ,请帮助

Try below code:- 试试下面的代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_1, container, false);
    String strtext = this.getArguments().getString("sessionName");    
    TextView sessionTitle = (TextView) view.findViewById(R.id.session1);
    sessionTitle.setText(strtext);
    return view;
}

your not getting view and try to set value on textview. 您没有获得视图,并尝试在textview上设置值。

You have 你有

 TextView sessionTitle = (TextView) getView()
        .findViewById(R.id.session1);

getView() returns null in your case. 在您的情况下, getView()返回null。 So sessionTitle is null and when you call setText() on null you get NUllPointerException . 所以sessionTitle为null,当您对null调用setText() ,会得到NUllPointerException

SO change to 所以变成

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = (VIew)inflater.inflate(R.layout.fragment_1, container, false); 
    // inflate tha layout
    String strtext = this.getArguments().getString("sessionName");   
    // get the string  
    TextView sessionTitle = (TextView) view
            .findViewById(R.id.session1);
     // initialize textview or initialize it in onActivityCreated using getView
    sessionTitle.setText(strtext);
    // set text to textview
    return view
}

You can use getView().findViewById(id) in onActivityCreated() 您可以在onActivityCreated()使用getView().findViewById(id) onActivityCreated()

while getting the id of textview you are getting nullpointer exception ,because of using getview(),getview is used when we get the id in onActivityCreated method(). 在获取textview的ID时,您会得到nullpointer异常 ,因为使用getview(),所以在onActivityCreated method()中获取ID时会使用getview。 just change getview() to view will remove this exception.just chnage : 只需更改getview()以查看将删除此异常。只需chnage:

TextView sessionTitle = (TextView)view.findViewById(R.id.session1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从活动发送到片段? - Sending data from activity to a fragment? 在选项卡式活动中将数据从片段发送到片段 - Sending Data to a Fragment from a Fragment in a Tabbed Activity 在通过数据包将数据从Activity传递到Fragment时获取null - Getting null while passing data through a bundle from Activity to Fragment 从Internet将活动中的数据发送到片段 - Sending data from Activity to Fragment from internet 将数据从片段发送到活动并解析json - Sending data from fragment to activity and parse json 使用接口将数据从片段发送到活动 - Sending data from a fragment to an activity using an interface ViewPager-将数据从片段发送到活动时,Android错误setCurrentItem(int,boolean) - ViewPager - Android Error setCurrentItem(int,boolean) while sending data from fragment to activity 将数据从活动发送和接收到扩展片段的另一个活动 - Sending and Receiving data from Activity to another activity that extends fragment 将数据从 Activity 1 中的 Fraggment_A 发送到 Activity 2 中的 Fragment_B - Sending data from Fraggment_A in Activity 1 to Fragment_B in Activity 2 从活动启动 Fragment 活动会导致 NullPointerException - Starting Fragment activity from an activity causes NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM