如何解决在android中使用intent从活动中接收null值的片段 [英] How to solve the fragment receiving null value from activity using intent in android

查看:95
本文介绍了如何解决在android中使用intent从活动中接收null值的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I send the values from Activity to Fragment. The Fragment receives Null value. How I solve this problem.







Code for Send Value in Activity







Intent in = new Intent(SpamActivity.this,InboxFragment.class);
                       Bundle extras = new Bundle();
                       extras.putString("number", sms.get(position).getmAddress());
                       in.putExtras(extras);







Code for Receive Value in Fragment







Bundle extras = getActivity().getIntent().getExtras();
       if (extras != null){
           String address = extras.getString("number");
           list.add(address);
       }else {
           System.out.println("Null");
       }







The Output is Null.





我尝试了什么:



我试过这个值使用捆绑发送。使用Bundle执行代码时出现空指针异常。



What I have tried:

I tried the value send by using bundle. The Null Pointer Exception occurs when executing code by using Bundle.

推荐答案

尝试 Intent.putExtra方法 [ ^ ]。


这篇关于如何解决在android中使用intent从活动中接收null值的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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