简体   繁体   中英

Null Pointer exception passing data from activity to Fragment

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");

Activity:

 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);
    }

Fragment:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM