【问题标题】:java.lang.IllegalArgumentException: invalid provider: null when openning MapsActivityjava.lang.IllegalArgumentException:无效的提供者:打开 MapsActivity 时为 null
【发布时间】:2017-02-23 10:38:14
【问题描述】:

我只是从项目中复制粘贴了部分代码

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

该活动应该提供一些 LOcationInformation,但是,当我启动该活动时,我的应用程序崩溃并显示以下错误消息:

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

我知道我没有很好地实现我的 Google API。 第一次,我将元数据添加到我的清单中:

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

并在我的项目结构依赖项中添加了“com.google.android.gms:play-services:7.5.0”。

知道,我不知道为什么我的应用程序崩溃了,你知道吗?

【问题讨论】:

标签: android google-maps-api-3


【解决方案1】:

之后

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

放这个元

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

【讨论】:

    【解决方案2】:

    确保将 android.permission.ACCESS_FINE_LOCATION 或 android.permission.ACCESS_COARSE_LOCATION 添加到您的 Manifest 文件中,这为我解决了问题(所有提供程序都已在位置设置中启用)。

    用户需要允许应用程序从 API 级别 23 开始运行位置权限,因此如果您使用的是 23 API,请执行以下操作

    if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            // No explanation needed, we can request the permission.
    
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_FINE_LOCATION);
    
            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
    } else {
            //Permission is granted
    }
    

    你也必须重写这个方法:

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_FINE_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
                    // permission was granted
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    showAlert(getString(R.string.error), getString(R.string.message));
                }
            }
    
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
    

    如果您可以降级 API 版本,请执行以下操作:

    如下所示更改 build.gradle:

    compileSdkVersion 21

    buildToolsVersion "21.1.2"

    希望对你有帮助。

    【讨论】:

    • 无法解析MY_PERMISSIONS_REQUEST_FINA_LOCATION,当你说它是一个'app-defined'常量时,你说我必须自己定义它?在哪个值?
    【解决方案3】:

    不要使用直接提供程序字符串,因为它不能保证它会在运行时存在。

    仅使用从 LocationManager 获得的提供程序,如下所示:

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

    并确保将 android.permission.ACCESS_FINE_LOCATION 或 android.permission.ACCESS_COARSE_LOCATION 添加到您的 Manifest 文件中。

    如果您正在运行您的代码,那么 Andoird 5.0 也会检查 premissions

    还可以尝试使用Fused Location API...位置管理器是获取位置的旧方法。

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      相关资源
      最近更新 更多