0

私のアプリケーションのテスト中に、GPSが有効になっているかどうかを確認する最良の方法を見つけようとしています。残念ながら、私は最良の方法を見つけることができませんでした。しかし、GPSが有効になっているときにコードが機能していましたが、現在は機能していません。これは、location.getLatitude()がnullオブジェクト参照であることを示しています。GPSが有効になっていてもgetLastKnownLocationがnullを返す

public class ThirdFragment extends Fragment implements OnMapReadyCallback { 
    View myView; 
    private GoogleMap mMap; 
    MapFragment mapFrag; 





    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView = inflater.inflate(R.layout.third_layout, container, false); 
     return myView; 

    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     mapFrag = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
     mapFrag.getMapAsync(this); 
     //mapFrag.onResume(); 

    } 


    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 


     if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
      Criteria criteria = new Criteria(); 
      LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
      String provider = locationManager.getBestProvider(criteria, false); 
      Location location = locationManager.getLastKnownLocation(provider); 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      LatLng coordinate = new LatLng(lat, lng); 
      CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13); 
      mMap.animateCamera(yourLocation); 

     } else { 
      final AlertDialog alertDialogGPS = new AlertDialog.Builder(getActivity()).create(); 

      alertDialogGPS.setTitle("Info"); 
      alertDialogGPS.setMessage("Looks like you have not given GPS permissions. Please give GPS permissions and return back to the app."); 
      alertDialogGPS.setIcon(android.R.drawable.ic_dialog_alert); 
      alertDialogGPS.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

        Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_SETTINGS); 
        startActivity(intentSettings); 
        alertDialogGPS.dismiss(); 
       } 

      }); 

      alertDialogGPS.show(); 
     } 

    } 
    @Override 
    public void onResume() { 
     super.onResume(); 
     if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 

      mapFrag.getMapAsync(this); 
     } 
     else { 
     } 
    } 
} 

ない、それは前に働いていたとき、フラグメントでこれをどのように処理するか、任意のアイデアを、ヌルを返している原因を確認してください:?ここ

は私が話していたコードですか

UPDATE:

私もこれを試みたが、まだそれを得る:私が得る

if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      mMap.setMyLocationEnabled(true); 
      Criteria criteria = new Criteria(); 
      LocationManager locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); 
      String provider = locationManager.getBestProvider(criteria, false); 
      if(locationManager != null) { 
       Location location = locationManager.getLastKnownLocation(provider); 
       double lat = location.getLatitude(); 
       double lng = location.getLongitude(); 
       LatLng coordinate = new LatLng(lat, lng); 
       CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 13); 
       mMap.animateCamera(yourLocation); 
      } else 
      { 
       locationManager.requestLocationUpdates(provider, 1000, 0, (LocationListener) this); 
      } 

     } 

エラーメッセージ:

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: com.example.jamessingleton.chffrapi, PID: 18112 
        java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 
         at com.example.jamessingleton.chffrapi.ThirdFragment.onMapReady(ThirdFragment.java:84) 
         at com.google.android.gms.maps.MapFragment$zza$1.zza(Unknown Source) 
         at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source) 
         at android.os.Binder.transact(Binder.java:387) 
         at xz.a(:com.google.android.gms.DynamiteModulesB:82) 
         at maps.ad.u$5.run(Unknown Source) 
         at android.os.Handler.handleCallback(Handler.java:739) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:158) 
         at android.app.ActivityThread.main(ActivityThread.java:7224) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found. 
W/ResourcesManager: getTopLevelResources: /data/app/com.google.android.gms-2/base.apk/1.0 running in com.example.jamessingleton.chffrapi rsrc of package com.google.android.gms 
I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:1 
I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 1 
D/ChimeraFileApk: Primary ABI of requesting process is arm64-v8a 
D/ChimeraFileApk: Classloading successful. Optimized code found. 
D/GoogleCertificates: com.google.android.gms.googlecertificates module is loaded 
D/GoogleCertificatesImpl: Fetched 163 Google release certificates 
D/GoogleCertificatesImpl: Fetched 318 Google certificates 
I/Process: Sending signal. PID: 18112 SIG: 9 
Application terminated. 

答えて

0

あなたは新しいものと古い場所APIの一緒に混合されていますあなたがすべきとき。ロケーションサービスが接続された後、すべてのあなたのしなければならない最後の既知の位置を取得する

は、コール

mLocationClient.getLastLocation(); 

です。 `または私はそれで何を交換しています。

は新しい場所のAPIを正確に私は` mLocationClient.getLastLocation()を入れています

Last location api

+0

の使い方をお読みになりましたか? –

+0

これは、以前のものが推奨されていないため、Fusion APIで使用されます。 LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)のように使用 –

+0

シンボル 'mGoogleApiClient'を解決できません –

関連する問題