2013-08-06 109 views
2

我想寫一個代碼,使應用程序要求用戶啓用提供程序來找到它...我試過這段代碼,但它崩潰當供應商將被禁用:要求用戶啓用GPS或網絡提供商... illegalargumentexception提供程序== null

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 

有人可以幫助我嗎?提前致謝。

+0

這是完整的日誌的變化?另外,請告訴您在哪一行發生異常。 – Nitish

+0

我現在把整個logcat;它不會告訴我我在哪一行得到例外。 – Delgado

+0

@Massimo是的。在117行。你可以粘貼一些你的MapActivity代碼嗎? (在com.example.findyourmosque.MapActivity.onCreate(MapActivity.java:117)) – Brayden

回答

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