要求用户启用GPS或网络提供商... illegalargumentexception provider == null

时间:2013-08-06 02:05:30

标签: 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
有人能帮帮我吗?提前谢谢。

1 个答案:

答案 0 :(得分:3)

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

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