1

Estou criando um app com 2 tabs em swipe, carregando Fragments. Sendo que o primeiro é um ListFragment (que está ok), o segundo deveria carregar um mapa, mas, depois que tentei inserir, dá crash na aplicação e exibe o erro:

    06-10 16:16:47.312  30209-30209/br.com.toyansk.swipeabletabs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: br.com.toyansk.swipeabletabs, PID: 30209
java.lang.NullPointerException
        at br.com.toyansk.swipeabletabs.MapaFragment$HttpGetTask.onPostExecute(MapaFragment.java:93)
        at br.com.toyansk.swipeabletabs.MapaFragment$HttpGetTask.onPostExecute(MapaFragment.java:62)
        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-10 16:16:47.316   1037-11772/? W/ActivityManager﹕ Force finishing activity br.com.toyansk.swipeabletabs/.MainActivity

O MainActivity está assim:

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

O MapaFragment, que deveria carregar o mapa, possui também funções para inserir marcadores no mapa através de dados de um JSON, e está assim:

public class MapaFragment extends Fragment {

Context mContext;
private GoogleMap mMap;
private Location location;

private final static String URL = "url da api que retorna em JSON os locais no mapa";

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

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

}
}

E o layout para o mapa está assim:

<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>

O que pode estar errado?

6
  • Pode ser por result ser null.
    – ramaral
    10/06/2015 às 19:47
  • Mas o result não está null... Quando eu insiro a url da api (String URL), retorna normalmente. Aqui eu omiti este dado e mais algumas classes para o carregamento JSON.
    – Atoyansk
    10/06/2015 às 21:04
  • O erro indica que está a usar um objecto que é nulo no método onPostExecute() da classe HttpGetTask, terá de encontrar qual é ele.
    – ramaral
    10/06/2015 às 21:14
  • Este erro passou a dar quando tentei "adaptar" estes métodos para dentro de um Fragment. Em um teste anterior, usando uma Activity, funcionou normal... Alguma ideia?
    – Atoyansk
    10/06/2015 às 21:22
  • Se postou todo o código do Fragment, o mContext não está ser inicializado.
    – ramaral
    10/06/2015 às 21:26

2 Respostas 2

0

Você não está a inicializar a variável mContext.
Faça o Override do metodo onAttach() do Fragment e inicialize-o lá:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = activity;
}
3
  • Acrescentei este Override, obrigado. Mas agora o erro é outro: Leak found java.lang.IllegalStateException: AndroidHttpClient created and never closed. Mas se reparar no código, há um if (null != mClient) mClient.close(); ao final do método onPostExecute.
    – Atoyansk
    10/06/2015 às 22:22
  • Não encontro nada de errado.
    – ramaral
    11/06/2015 às 8:07
  • Estranhamente, hoje rodei de novo e apresentou este erro: FATAL EXCEPTION: main Process: br.com.toyansk.swipeabletabs, PID: 12309 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). A linha 100 é esta: mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();. Como solucionar?
    – Atoyansk
    11/06/2015 às 17:17
0

Baseado no post https://stackoverflow.com/questions/19353255 (vejam resposta de Brandon Yang), alterei o código da MapaFragment.java, usando MapView, e funcionou. Ficou assim:

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

}
}

E no 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" />

Talvez falte organizar melhor o código, mas assim o mapa carregou perfeito no Fragment.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .