error java.lang.IllegalArgumentException:invalid provider:null

时间:2017-05-30 07:42:51

标签: android-studio locationmanager

谁能帮帮我?我收到错误java.lang.IllegalArgumentException:invalid provider:null。怎么解决?如果我在Android版本4.4.4中运行它没关系,但如果我在Android版本6.0.1中运行,它就会强行关闭。

这是我的代码:

// fungsi ngecek lokasi GPS device pengguna
private void lokasi(){
    location = locationManager.getLastKnownLocation(provider);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    // permintaan update lokasi device dalam waktu 10 detik
    locationManager.requestLocationUpdates(provider, 10000, 1, this);
    if(location!=null){
        onLocationChanged(location);
        callListVolley(latitude, longitude);
    }else{
        Toast.makeText(this, "Lokasi device pengguna tidak ditemukan.\nMohon hidupkan GPS.",Toast.LENGTH_LONG).show();
        swipe.setRefreshing(false);
    }
}

我还在清单中添加了android.permission.ACCESS_COARSE_LOCATION和android.permission.ACCESS_FINE_LOCATION

1 个答案:

答案 0 :(得分:0)

我有一些问题。我解决它就像轰鸣声

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

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    // your codes
}

用户需要允许应用程序的位置权限从API级别23开始运行。