LocationManagerはNullPointerExpectionをスローします

クワージェイ

私はこの間ずっと私のために働いていたこのコードを持ってください、突然それはもう働いていません、それはnull pointer exception戻ります

double lat = (double) (lastKnownLocation.getLatitude());.

マニフェストファイルで必要なすべての権限を設定しましたが、再び機能しないようです。今GPSプロバイダーを使うのに問題があるに違いないと思います。

    latituteField = (TextView) findViewById(R.id.TextViewLatitudeValue);
    longitudeField = (TextView) findViewById(R.id.TextViewLongitudeValue);
    SpeedField = (TextView) findViewById(R.id.textViewSpeedValue);
    AltitudeField = (TextView) findViewById(R.id.textViewAltitudeValue);        
    AddressLabel=(TextView)findViewById(R.id.textViewAddress);
    lbllatitude=(TextView)findViewById(R.id.lblLatDMS);
    lbllongitude=(TextView)findViewById(R.id.lblLongDMS);



    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // GPS_PROVIDER
    String locationProvider = LocationManager.GPS_PROVIDER;  
    Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

    // check if enabled and if not send user to the GSP settings
    // Better solution would be to display a dialog and suggesting to 
    // go to the settings
    if (!enabled) 
    {           
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);

    }
    else if (locationProvider!= null) 
    {
     Toast.makeText(this, locationProvider +" has been selected",Toast.LENGTH_SHORT).show();
      onLocationChanged(lastKnownLocation);
    } 
    else 
    {
      latituteField.setText("0.00");
      longitudeField.setText("0.00");
      AltitudeField.setText("0.00");
      SpeedField.setText("0.00");

    }

  }

  /* Request updates at startup */
  @Override
  protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
  }

  /* Remove the locationlistener updates when Activity is paused */
  @Override
  protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
  }

  @Override
  public void onLocationChanged(Location lastKnownLocation) {

    double lat = (double) (lastKnownLocation.getLatitude());
    double lng = (double) (lastKnownLocation.getLongitude());
    double Alt = (double) (lastKnownLocation.getAltitude());
    double Speed = (double) (lastKnownLocation.getSpeed());

    latituteField.setText(String.format("%.5f",lat));
    longitudeField.setText(String.format("%.5f",lng));
    AltitudeField.setText(String.format("%.5f",Alt));
    SpeedField.setText(String.format("%.5f",Speed));
ガベセチャン

LastKnownLocationは、場所が古すぎる場合、またはプロバイダーがオンにされたことがない場合、nullを返します。これを考慮に入れる必要があります。有効な場所を確実に取得する唯一の方法は、更新を要求してonLocationChangedで取得することです。

また、lastKnownLocationが非常に間違った値を返す場合があります。10分または15分前の値が返される可能性があります。あなたはそれを使うことができますが、正確さを期待しないでください。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

locationManager.locationはnilを返します

分類Dev

locationManager.getLastKnownLocation()はnullを返します

分類Dev

アラートダイアログ内でlayout(toggleButton)を使用すると、常にnullPointerExpectionが返されます

分類Dev

LocationManagerは常に最後の場所を送信しています

分類Dev

LocationManager.GPS_PROVIDERは0.0を返します

分類Dev

LocationManager.GPS_PROVIDERは0.0を返します

分類Dev

LocationManagerは、AndroidのgetLastKnownLocationでnull経度を返します

分類Dev

getLastKnownLocation(LocationManager.NETWORK_PROVIDER)はlocation(1.0、1.0)を返します

分類Dev

Wekaは「UnassignedDatasetException」をスローします

分類Dev

Wekaは「UnassignedDatasetException」をスローします

分類Dev

leiningennewは例外をスローします

分類Dev

startSupportActionMode()はNullPointerExceptionをスローします

分類Dev

UseHangfireServerは例外をスローします

分類Dev

PeripheralManagerServiceはNoClassDefFoundErrorをスローします

分類Dev

rxjavaはOnErrorNotImplementedExceptionをスローします

分類Dev

IconButtonは例外をスローします

分類Dev

CVXPYはSolverErrorをスローします

分類Dev

AspNetCoreNodeServicesはNodeInvocationExceptionをスローします

分類Dev

InputStreamReaderはNullPointerExceptionをスローします

分類Dev

DataTemplateSelectorCreateContentはInvalidOperationExceptionをスローします

分類Dev

AngularはExpressionChangedAfterItHasBeenCheckedErrorをスローします

分類Dev

SockJsClientはClassCastExceptionをスローします

分類Dev

MVVMRaisePropertyChangedはInvalidCastExceptionをスローします

分類Dev

BigIntegerはnumberFormatExceptionをスローします

分類Dev

SqljockyはSocketExceptionをスローします

分類Dev

setContentViewはNullPointerExceptionをスローします

分類Dev

HttpUrlConnectiongetOutputStreamはIOExceptionをスローします

分類Dev

getDeclaredMethodは例外をスローします

分類Dev

popToViewControllerはNSRangeExceptionをスローします

Related 関連記事

  1. 1

    locationManager.locationはnilを返します

  2. 2

    locationManager.getLastKnownLocation()はnullを返します

  3. 3

    アラートダイアログ内でlayout(toggleButton)を使用すると、常にnullPointerExpectionが返されます

  4. 4

    LocationManagerは常に最後の場所を送信しています

  5. 5

    LocationManager.GPS_PROVIDERは0.0を返します

  6. 6

    LocationManager.GPS_PROVIDERは0.0を返します

  7. 7

    LocationManagerは、AndroidのgetLastKnownLocationでnull経度を返します

  8. 8

    getLastKnownLocation(LocationManager.NETWORK_PROVIDER)はlocation(1.0、1.0)を返します

  9. 9

    Wekaは「UnassignedDatasetException」をスローします

  10. 10

    Wekaは「UnassignedDatasetException」をスローします

  11. 11

    leiningennewは例外をスローします

  12. 12

    startSupportActionMode()はNullPointerExceptionをスローします

  13. 13

    UseHangfireServerは例外をスローします

  14. 14

    PeripheralManagerServiceはNoClassDefFoundErrorをスローします

  15. 15

    rxjavaはOnErrorNotImplementedExceptionをスローします

  16. 16

    IconButtonは例外をスローします

  17. 17

    CVXPYはSolverErrorをスローします

  18. 18

    AspNetCoreNodeServicesはNodeInvocationExceptionをスローします

  19. 19

    InputStreamReaderはNullPointerExceptionをスローします

  20. 20

    DataTemplateSelectorCreateContentはInvalidOperationExceptionをスローします

  21. 21

    AngularはExpressionChangedAfterItHasBeenCheckedErrorをスローします

  22. 22

    SockJsClientはClassCastExceptionをスローします

  23. 23

    MVVMRaisePropertyChangedはInvalidCastExceptionをスローします

  24. 24

    BigIntegerはnumberFormatExceptionをスローします

  25. 25

    SqljockyはSocketExceptionをスローします

  26. 26

    setContentViewはNullPointerExceptionをスローします

  27. 27

    HttpUrlConnectiongetOutputStreamはIOExceptionをスローします

  28. 28

    getDeclaredMethodは例外をスローします

  29. 29

    popToViewControllerはNSRangeExceptionをスローします

ホットタグ

アーカイブ