Android Studio java.lang.IllegalArgumentException: invalid provider: null

Asked 1 years ago, Updated 1 years ago, 46 views

package com.example.jun.artest;

import android.Manifest;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

/**
 * * Created by Jun on 2017-05-07.
 */

public class MainActivity extends Activity {

    private static final String TAG = "Compass";
    private static boolean DEBUG = true;
    private SensorManager mSensorManager;
    private Sensor mSensor;
    LocationManager locMgr;

    private DrawSurfaceView mDrawView;

    private final SensorEventListener mListener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent event) {
            if (mDrawView != null) {
                mDrawView.setOffset(event.values[0]);
                mDrawView.invalidate();
            }
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);


        locMgr = (LocationManager) this.getSystemService(LOCATION_SERVICE); // <2>
        locMgr = (LocationManager) this.getSystemService(Service.LOCATION_SERVICE);

        // // getting GPS status
        boolean isGPSEnabled = locMgr
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // // getting network status
        boolean isNetworkEnabled = locMgr
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.d("OnCreate", LocationUtils.createFineCriteria().toString());
        Log.d("******GPS******", String.valueOf(isGPSEnabled));
        Log.d("******NETWORK*******", String.valueOf(isNetworkEnabled));

        if (ContextCompat.checkSelfPermission
                (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 0);
        } } else {
            Log.d("PerMission", "ACCESS_FINE_LOCATION GRANTED******************");
        }

        LocationProvider high = locMgr.getProvider(locMgr.getBestProvider(LocationUtils.createFineCriteria(), true));
        // // using high accuracy provider... to listen for updates
        try {
            locMgr.requestLocationUpdates(high.getName(), 0, 0f,
                    new LocationListener() {
                        public void onLocationChanged(Location location) {
                            // // do something here to save this new location
                            Log.d(TAG, "Location Changed");
                            mDrawView.setMyLocation(location.getLatitude(), location.getLongitude());
                            mDrawView.invalidate();
                        }

                        public void onStatusChanged(String s, int i, Bundle bundle) {

                        }

                        public void onProviderEnabled(String s) {
                            // // try switching to a different provider
                        }

                        public void onProviderDisabled(String s) {
                            // // try switching to a different provider
                        }
                    });
        } } catch(SecurityException e){

        }
        Log.d("********************", "WOWOWOWWOWOWOW*******************8");
    }

    @Override
    protected void onResume() {
        if (DEBUG)
            Log.d(TAG, "onResume");
        super.onResume();

        mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop() {
        if (DEBUG)
            Log.d(TAG, "onStop");

        mSensorManager.unregisterListener(mListener);
        super.onStop();
    }
}

05-07 21:18:03.818 19859-19859/com.example.jun.artest E/AndroidRuntime: FATAL EXCEPTION: main

                                                                    Process: com.example.jun.artest, PID: 19859
                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jun.artest/com.example.jun.artest.MainActivity}: java.lang.IllegalArgumentException: invalid provider: null
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
                                                                        at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6692)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
                                                                     Caused by: java.lang.IllegalArgumentException: invalid provider: null
                                                                        at android.location.LocationManager.checkProvider(LocationManager.java:2248)
                                                                        at android.location.LocationManager.getProvider(LocationManager.java:381)
                                                                        at com.example.jun.artest.MainActivity.onCreate(MainActivity.java:79)
                                                                        at android.app.Activity.performCreate(Activity.java:6912)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) 
                                                                        at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                        at android.os.Looper.loop(Looper.java:154) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6692) 
                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 

Code to retrieve location from Android Location Manager.

I keep getting the same error as above, but I don't know why the error is coming up.

isGPSEnabled and isNetworkEnabled to confirm that there is no problem with the Permission,,

I don't know why there's a null argumentPlease look at the code.

There are several files that I wrote, but I omitted them because I thought they would be too long.

android-studio

2022-09-22 19:55

2 Answers

First, make sure that AndroidManifest.xml has the following permissions added:

<manifest ... >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    ...
    <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
    <uses-feature android:name="android.hardware.location.gps" />
    ...
</manifest>

ACCESS_FILE_LOCATION means that you will receive location information updates from NETWORK_PROVIDER and GPS_PROVIDER.

The return of null in the locMgr.getBestProvider() function among the codes you uploaded means that no provider is available. It is possible that the user has disabled all providers, or that the Criteria created with LocationUtils.createFineCriteria() is an object that meets the requirements.

According to the code below, if an exception occurs, it can be confirmed that GPS and wireless networks are disabled and check the current status of the device.

In any case, this function can return null, so exceptional treatment is required for this part.


2022-09-22 19:55

I'm not sure because I haven't tried it

After searching, it says it's because there are two and it's not set to API 23 or higher.

I think you can edit it and try it again.


2022-09-22 19:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.