Load Maps within Fragment

478 views Asked by At

I am developing an app with 2 tabs on swipe, each one loading its Fragment. The first of them is a ListFragment (which it's ok), and the second should load a map, but when I tried to insert Google Map into this Fragment, it crashes the application and displays the error:

06-11 18:11:52.973  32676-32676/br.com.toyansk.swipeabletabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: br.com.toyansk.swipeabletabs, PID: 32676
java.lang.NullPointerException
        at br.com.toyansk.swipeabletabs.MapaFragment$HttpGetTask.onPostExecute(MapaFragment.java:100)
        at br.com.toyansk.swipeabletabs.MapaFragment$HttpGetTask.onPostExecute(MapaFragment.java:69)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
06-11 18:11:52.980    1037-3188/? W/ActivityManager﹕ Force finishing activity br.com.toyansk.swipeabletabs/.MainActivity

The MainActivity.java is:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
List<Fragment> mFragments;

// Tab titles
private String[] tabs = { "Lista", "Mapa" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(), mFragments);
//mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

mFragments = new ArrayList<Fragment>();
mFragments.add(new List_Fragment());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


final int[] ICON = new int[] {
        R.drawable.ic1,
        R.drawable.ic2
};
// Adding Tabs
for (int i=0; i < tabs.length; i++) {
    actionBar.addTab(actionBar.newTab()
            .setIcon(getResources().getDrawable(ICON[i]))
            .setTabListener(this));
}

/**
 * on swiping the viewpager make respective tab selected
 * */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        // on changing the page
        // make respected tab selected
        actionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
   // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}

The MapaFragment.java, which should load the map, it also has functions to insert markers on the map using data from a JSON, and it's as follows:

public class MapaFragment extends Fragment {

Context mContext;
private GoogleMap mMap;
private Location location;

private final static String URL = "api that returns maps data in JSON";
public static final String TAG = "MapaFragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);

new HttpGetTask().execute(URL);

return rootView;
}

@Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = activity;
}

private class HttpGetTask extends AsyncTask<String, Void, List<PDVRec>> {

AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

@Override
protected List<PDVRec> doInBackground(String... params) {

    HttpGet request = new HttpGet(params[0]);
    JSONResponseHandler responseHandler = new JSONResponseHandler();

    try {

        // Get data in JSON format

        return mClient.execute(request, responseHandler);

    } catch (ClientProtocolException e) {
        Log.i(TAG, "ClientProtocolException");
    } catch (IOException e) {
        Log.i(TAG, "IOException");
    }

    return null;

}

@Override
protected void onPostExecute(List<PDVRec> result) {

    // Get Map Object
    mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();


    if (null != mMap) {
        // Add a marker for every earthquake
        for (PDVRec rec : result) {
            // Add a new marker for this earthquake
            mMap.addMarker(new MarkerOptions()
                    // Set the Marker's position
                    .position(new LatLng(rec.getLat(), rec.getLng()))
                            // Set the title of the Marker's information window
                    .title(String.valueOf(rec.getNome_PDV()))
                            // Set the icon for the Marker
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.currentlocation_icon)));

        }

        LocationManager locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
        Criteria criteria = new Criteria();

        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
        if (location != null)
        {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(location.getLatitude(), location.getLongitude()), 17));

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    // Sets the center of the map to location user
                    .target(new LatLng(location.getLatitude(), location.getLongitude()))
                    .zoom(17)                   // Sets the zoom
                            //.bearing(90)              // Sets the orientation of the camera to east
                            //.tilt(40)                 // Sets the tilt of the camera to 30 degrees
                    .build();                  // Creates a CameraPosition from the builder
            mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }

        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);

    }

    if (null != mClient)
        mClient.close();
}
}
}

And the Map's layout (fragment_mapa.xml) is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    android:id="@+id/container">
<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

What could be wrong? Thanks in advance!

1

There are 1 answers

0
Atoyansk On BEST ANSWER

Based on post How to put Google Maps V2 on a Fragment Using ViewPager (see Brandon Yang's answer), I changed the MapaFragment.java code using MapView, and it worked. Like this:

public class MapaFragment extends Fragment {

Context mContext;
private GoogleMap mMap;
MapView mMapView;
private Location location;

private final static String URL = "api que retorna JSON";

public static final String TAG = "MapaFragment";

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);

        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMap = mMapView.getMap();

    new HttpGetTask().execute(URL);

    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = activity;
}

@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

private class HttpGetTask extends AsyncTask<String, Void, List<PDVRec>> {

    AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

    @Override
    protected List<PDVRec> doInBackground(String... params) {

        HttpGet request = new HttpGet(params[0]);
        JSONResponseHandler responseHandler = new JSONResponseHandler();

        try {

            // Get data in JSON format
            // Parse data into a list

            return mClient.execute(request, responseHandler);

        } catch (ClientProtocolException e) {
            Log.i(TAG, "ClientProtocolException");
        } catch (IOException e) {
            Log.i(TAG, "IOException");
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<PDVRec> result) {

        if (null != mMap) {
            for (PDVRec rec : result) {
                mMap.addMarker(new MarkerOptions()
                        // Set the Marker's position
                        .position(new LatLng(rec.getLat(), rec.getLng()))
                                // Set the title of the Marker's information window
                        .title(String.valueOf(rec.getNome_PDV()))
                                // Set the icon for the Marker
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.currentlocation_icon)));

            }

            LocationManager locationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
            Criteria criteria = new Criteria();

            Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
            if (location != null)
            {
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                        new LatLng(location.getLatitude(), location.getLongitude()), 17));

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        // Sets the center of the map to location user
                        .target(new LatLng(location.getLatitude(), location.getLongitude()))
                        .zoom(17)                   // Sets the zoom
                        .build();                  // Creates a CameraPosition from the builder
                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            }

            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(false);

        }

        if (null != mClient)
            mClient.close();
    }

}
}

And the fragment_mapa.xml:

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Perhaps need to better organize the code, but this way the map loaded perfect at the Fragment.