-2

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");
raj
  • 17
  • 9
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Dec 02 '14 at 10:44
  • Move `transaction.commit();` after `fragobj.setArguments(bundle);` – M D Dec 02 '14 at 11:09

1 Answers1

2

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(); 
Renan Ferreira
  • 2,122
  • 3
  • 21
  • 30