2

プロバイダーがそれを見つけられるようにすることをユーザーに要求するアプリケーションを作成するコードを作成しようとしています...このコードを試しましたが、プロバイダーが無効になっているとクラッシュします:

   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

誰かが私を助けることができますか?前もって感謝します。

4

1 に答える 1

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);
                 }
        }
于 2013-08-06T02:50:01.910 に答える