空指针异常将数据从活动传递到片段 [英] Null Pointer exception passing data from activity to Fragment

查看:110
本文介绍了空指针异常将数据从活动传递到片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MainActivity页面中有url值,我需要将此URL值从ACtivity传递到Fragment.但是,当我单击按钮时,在String url=getArguments().getString("btn_url");

I have the url value in the MainActivity page and i need to pass this url value from ACtivity to Fragment. But when i click the button i'm getting null exception on the line of String url=getArguments().getString("btn_url");

活动:

 Fragment fragment=new About();                             
        if (fragment != null) {

     FragmentTransaction transaction = getFragmentManager().beginTransaction();
       transaction.replace(R.id.frame_container, fragment, "TAG_FRAGMENT");
        transaction.addToBackStack(null);
        transaction.commit(); 

          Bundle bundle=new Bundle();
          bundle.putString("btn_url", URL);
About fragobj=new About();
     fragobj.setArguments(bundle);
    }

片段:

String url=getArguments().getString("btn_url");

推荐答案

设置碎片参数后,需要提交. 将您的代码更改为此:

You need to commit after you set the fragments arguments. Change your code to this:

About fragobj=new About();    
Bundle bundle=new Bundle();
bundle.putString("btn_url", URL);        
fragobj.setArguments(bundle);
getFragmentManager().beginTransaction();
.replace(R.id.frame_container, fragobj, "TAG_FRAGMENT");
.addToBackStack(null);
.commit(); 

这篇关于空指针异常将数据从活动传递到片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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