AndroidNewbie
AndroidNewbie

Reputation: 31

java.lang.IllegalArgumentException: invalid provider: null when openning MapsActivity

I just copy-pasted part of code from a project

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class MapsActivity extends Activity implements LocationListener {

private GoogleMap mMap;         // Null si les services Google Play APK ne sont pas disponible.

// Ma position par defaut pour test
private static final LatLng position_gps = new LatLng(43.701397, 1.838987);

// Positions pour test des trackers
private static final LatLng position_gps_track1 = new LatLng(43.602577, 1.362320);
private static final LatLng position_gps_track2 = new LatLng(43.602529, 1.362634);
private static final LatLng position_gps_track3 = new LatLng(43.602349, 1.362681);
private static final LatLng position_gps_track4 = new LatLng(43.602767, 1.362435);

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

    final ListView listview = (ListView) findViewById(R.id.listViewMap);
    String[] values = new String[]{"00:44 - Alert Area", "10:24 - Connect Lost", "10:35 - BLE true", "10:40 - Alert Area", "10:49 - Alert Area", "11:43 - Connect Lost", "11:57 - BLE true"};

    final ArrayList<String> list = new ArrayList<String>();
    for (int i = 0; i < values.length; ++i) {
        list.add(values[i]);
    }

    final StableArrayAdapter adapter = new StableArrayAdapter(this,
            R.layout.list_view_ligne, list);
    listview.setAdapter(adapter);
    listview.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);

    LocationManager locationManager;

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    // Recupere le nom du meilleur fournisseur disponible
    String provider = locationManager.getBestProvider(criteria, true);

    // Recupere la derniere position connue

    Location location = locationManager.getLastKnownLocation(provider);
    LatLng pos_gps;
    if (location == null) {    // SI on arrive pas a recuperer la position GPS
        pos_gps = position_gps;     // Met la position GPS par defaut
    } else {                    // SINON recupere la position courante
        pos_gps = new LatLng(location.getLatitude(), location.getLongitude());
    }

    // Recupere la carte
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    if (mMap != null) {
        // Ajout des trackers sur la carte
        mMap.addMarker(new MarkerOptions().position(position_gps_track1).title("T1 - Thomas"));
        mMap.addMarker(new MarkerOptions().position(position_gps_track2).title("T2 - Paul"));
        mMap.addMarker(new MarkerOptions().position(position_gps_track3).title("T3 - Sebastien"));
        mMap.addMarker(new MarkerOptions().position(position_gps_track4).title("T4 - Marc"));

        // Affiche la carte par rapport a la position courante
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos_gps, 15));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        mMap.setMyLocationEnabled(true);                        // Affiche le bouton Ma Position
        mMap.getUiSettings().setCompassEnabled(true);           // Affiche la boussole
        mMap.getUiSettings().setZoomControlsEnabled(true);      // Affiche les boutons de Zoom
    } else {
        Toast.makeText(this, "Services GoogleMap indisponibles!", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onLocationChanged(Location location) {

    // Met a jour la position
    int latitude = (int) (location.getLatitude());
    int longitude = (int) (location.getLongitude());
    LatLng position_gps = new LatLng(location.getLatitude(), location.getLongitude());
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position_gps, 15));
}

private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
                              List<String> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}
}

This activity is supposed to give some LOcationInformation, but, when i start the activity, my application crash with this error message :

FATAL EXCEPTION: main
   Process: package.connectivapp, PID: 7694
   java.lang.RuntimeException: Unable to start activity ComponentInfo{package.connectivapp/package.connectivapp.MapsActivity}: java.lang.IllegalArgumentException: invalid provider: null
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    Caused by: java.lang.IllegalArgumentException: invalid provider: null
    at android.location.LocationManager.checkProvider(LocationManager.java:2248)
    at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1199)
    at package.connectivapp.MapsActivity.onCreate(MapsActivity.java:72)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
02-23 09:20:41.705 1589-2845/system_process W/ActivityManager:   Force finishing activity package.connectivapp/.MapsActivity
02-23 09:20:41.712 1589-2845/system_process W/ActivityManager:   Force finishing activity package.connectivapp/.MainActivity

I know that i didn't implement my Google API in a good way. In the first time, I added meta data to my Manifest :

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="[A Big String]" />

and added "com.google.android.gms:play-services:7.5.0" in my Project Structure dependencies.

Know, i don't know why my application crashes, have you any idea ?

Upvotes: 3

Views: 7450

Answers (2)

Bhavin Sorathiya
Bhavin Sorathiya

Reputation: 161

after

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="[A Big String]" />

put this meta

<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

Upvotes: 1

Umer
Umer

Reputation: 1556

Do not use direct provider strings, as it is not guaranteed that it will exist at runtime.

Use only providers that you get from the LocationManager, like this:

LocationManager locationManager = (LocationManager)context.getSystemService( Context.LOCATION_SERVICE );

        Criteria criteria = new Criteria();
        criteria.setAccuracy( Criteria.ACCURACY_COARSE );
        String provider = locationManager.getBestProvider( criteria, true );

        if ( provider == null ) {
            Log.e( TAG, "No location provider found!" );
            return;
        }

        lastLocation = locationManager.getLastKnownLocation(provider);

And make sure to add android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_COARSE_LOCATION to your Manifest file.

And if you are running your code greater then Andoird 5.0 check for premissions as well.

And also try to use Fused Location API...Location Manager is old way to get locations.

Upvotes: 0

Related Questions