1

I am working with a navigation drawer on a MainActivity and then I am using this navigation drawer to replace different fragments int the container of the MainActivity. In Fragment I am using Google Map. In this fragment I am also using the Ion library to download some location information from the server and to show them on the map.

The navigation drawer items are Home ,friends location , about us

now when I click on friend location I want to draw pins on the map , so I am u using the same map of the home screen as home screen is also showing map.

What I am doing on Friends location item click is as under

myMapFragment = new MyMapFragment();

                        fragmentTransaction1.replace(R.id.frame, myMapFragment, "MyMapFragment").commit();
                        fragmentManager.executePendingTransactions();
                        myMapFragment.GetAllFriendsLocation("share",MainActivity.this);

so the GetAllFriendsLocation is the method in that fragment which is showing map and this function is downloading location information from server and then draw pins on the map

this is how I am doing this

 public void GetAllFriendsLocation(String type) {
        MainActivity.setOverflowButtonColor(getActivity(),2);
        if(cd.isConnectingToInternet()) {
            progressDialog.show();
            Ion.with(getActivity())
                    .load("http://myapi.com/data.php")...

but I am getting nulpointerException on the following line

Ion.with(getActivity()) .load("http://myapi.com/data.php").

it seems like that getActivity is empty as the fragment is not fully initiated.

So please give me advises that how can I call the fragment method when it is initialized and ready to show on the screen , after when it is initialized the method should run and download from server.

or second this i can think of is , How can I get context in mainactiivty before the fragment is initialized ???

please help me in this ..

Coas Mckey
  • 701
  • 1
  • 13
  • 39
  • why not just pass the app context to ion? – Bhargav Sep 13 '15 at 18:44
  • @bhargav what do you mean , please explain – Coas Mckey Sep 13 '15 at 18:58
  • getActivity().getApplicationContext(), this is never going to be null, and Ion uses context to tie its network request threads to its lifecycle so by passing applicationCpntext() the only difference is the Network request thread will be running even if the activity onDestroy() was called, as long as the applicationContext is stil active. – Bhargav Sep 14 '15 at 07:22
  • i am passing the application context from the mainactivity while calling the fragment function , let say myMapFragment.GetAllFriendsLocation("locations",this.getApllicationContext) and fetching this context in mapFragment 's function in the following way public static void GetAllFriendsLocation(String type, Context context) but still gets the null pointer exception on Ion.with(context)................... – Coas Mckey Sep 14 '15 at 07:39
  • dont you think as the mapfragment is not fully initialized thats is why I am getting the error ? – Coas Mckey Sep 14 '15 at 07:39
  • So you are telling me `MainActivity.setOverflowButtonColor(getActivity(),2);` line doesnt throw nullpointer but `Ion.with(getActivity()) .load("http://myapi.com/data.php")...` does ? – Bhargav Sep 14 '15 at 07:48

3 Answers3

0

Try Creating and using Fragment using instance instead of constructor. Create an instance of fragment in the fragment class itself like this:

public static MyMapFragment newInstance(Bundle bundle) {
    MyMapFragment fragment = new MyMapFragment ();

    fragment.setArguments(bundle);
    return fragment;
}

And then in your activity use it like this:

MyMapFragment fragment = MyMapFragment.getInstance(bundle);

where bundle contains any thing you might wish to send to fragment. This should work.

Saurabh
  • 434
  • 1
  • 4
  • 19
  • replace `myMapFragment = new MyMapFragment();` by `MyMapFragment fragment = MyMapFragment.getInstance(bundle);` and paste the getInstance() function in your fragment class. Remove the bundle argument if you don't need it. – Saurabh Sep 14 '15 at 06:47
0

You should use onCreate() and onViewCreated() methods.

You load a fragment inside onCreate() method. and call method in onViewCreated() method.

Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
0

I think it's similar to here android get activity returns null

As my opinion, before doing something with getActivity you should check your Fragment isSafe or not safe like: (recommend)

private boolean isSafe()
{
    if (this.isRemoving() || this.getActivity() == null || this.isDetached() || !this.isAdded()
                    || this.getView() == null)
    {
        return false;
    }

    return true;
}

Or cache your activity like

private Activity mActivity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.mActivity = activity;
}
Community
  • 1
  • 1
Tan Tran
  • 166
  • 1
  • 6