android - 要求用户启用 GPS 或网络提供商...illegalargumentException 提供商==null

标签 android gps android-contentprovider

我正在尝试编写一段代码,使应用程序要求用户允许提供程序找到它...我尝试了此代码,但当提供程序被禁用时它会崩溃:

   SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

                // Getting GoogleMap object from the fragment
                map = fm.getMap();

                // Enabling MyLocation Layer of Google Map
                map.setMyLocationEnabled(true); 

                 // Getting LocationManager object from System Service LOCATION_SERVICE
                if(locationManager==null)
                locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                try{
                    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                    }catch(Exception ex){}
                    try{
                    network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                    }catch(Exception ex){}

                   if(!gps_enabled && !network_enabled){
                       Builder  dialog = new AlertDialog.Builder(this);
                       dialog.setMessage("Activez l'un des ressources de localisation" );
                        dialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {


                            @Override
                            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                                // TODO Auto-generated method stub
                                startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
                            }
                        });
                        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                                // TODO Auto-generated method stub

                            }
                        });
                        dialog.show();

                   }
                // Creating a criteria object to retrieve provider
                Criteria criteria = new Criteria();

                // Getting the name of the best provider
                 provider = locationManager.getBestProvider(criteria, true);

                // Getting Current Location
                Location location = locationManager.getLastKnownLocation(provider);

                if(location!=null){
                        onLocationChanged(location);
                }

                locationManager.requestLocationUpdates(provider, 20000, 0, this);

            }

        }

这是 logcat

08-06 02:59:16.609: E/AndroidRuntime(20881): FATAL EXCEPTION: main
08-06 02:59:16.609: E/AndroidRuntime(20881): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.find/com.example.MapActivity}: java.lang.IllegalArgumentException: provider==null
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1808)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:737)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.widget.TabHost.setCurrentTab(TabHost.java:401)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:540)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.view.View.performClick(View.java:3534)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.view.View$PerformClick.run(View.java:14263)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.os.Handler.handleCallback(Handler.java:605)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.os.Looper.loop(Looper.java:137)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.ActivityThread.main(ActivityThread.java:4441)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at java.lang.reflect.Method.invoke(Method.java:511)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at dalvik.system.NativeStart.main(Native Method)
08-06 02:59:16.609: E/AndroidRuntime(20881): Caused by: java.lang.IllegalArgumentException: provider==null
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1028)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at com.example.findyourmosque.MapActivity.onCreate(MapActivity.java:117)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.Activity.performCreate(Activity.java:4465)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-06 02:59:16.609: E/AndroidRuntime(20881):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
08-06 02:59:16.609: E/AndroidRuntime(20881):    ... 18 more

有人可以帮助我吗?提前致谢。

最佳答案

我找到了解决方案,希望可以帮助其他人 这是我所做的更改

  if(locationManager==null)
                locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            try{
            gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            }catch(Exception ex){}
            try{
            network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            }catch(Exception ex){}

           if(!gps_enabled && !network_enabled){
               Builder  dialog = new AlertDialog.Builder(this);
               dialog.setMessage("Activez l'un des ressources de localisation" );
                dialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {


                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        // TODO Auto-generated method stub
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 100);
                    }
                });
                dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                        // TODO Auto-generated method stub

                    }
                });
                dialog.show();

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


            Criteria crit = new Criteria();
            crit.setPowerRequirement(Criteria.POWER_LOW);
         crit.setAccuracy(Criteria.ACCURACY_COARSE);
            provider = locationManager.getBestProvider(crit, false);

            Criteria criteria = new Criteria();

            provider = locationManager.getBestProvider(criteria, false);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            location = locationManager.getLastKnownLocation(provider);



               if(location!=null)
                {
                   onLocationChanged(location);
                 }
        }

关于android - 要求用户启用 GPS 或网络提供商...illegalargumentException 提供商==null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18070705/

相关文章:

ios - 在 iOS MapKit 中通过经纬度坐标判断国家

android - 谷歌地图 - 位置不断在我的真实位置和 0.6 英里外(随机位置)之间跳跃

安卓 : Update exicting Contact List

javascript - Uncaught ReferenceError : PhoneGap is not Defined

android - android xpath 中的 "TransformerException: A location step was expected"

gps - 解码SMS报告肯尼亚狮子的GPS位置

android - 从 contentProvider.update() 更改 uri 的 notifyChange

android - 使用 ContentResolver.applyBatch() 的主详细信息?

java - 设置textView时Fragment中的NPE

android - 通过集合中提供的 ID 在 Realm 中查询对象