2017-03-29 10 views
0

私はすべての答えを試して、私のために働いていないので、私はこれをもう一度尋ねている唯一の理由は私が取得しようとしている現在のユーザーの場所が動作していないので、これは私がやろうとしてきたことです.btwはすでにすべてのアクセス許可を設定しています。そして、nullオブジェクト参照エラーと致命的な実行メインでダブルアンドロイドのlocation.getlatitudeを取得し続けます。double android.location.Location.getLatitude() 'nullのオブジェクト参照で

package com.example.nefissa.androidtest; 

import android.*; 
import android.Manifest; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.support.annotation.Nullable; 
import android.support.v4.content.ContextCompat; 


public class GPSTracker extends Service implements LocationListener{ 

private final Context context; 

boolean isGPSEnabled =false; 
boolean isNetworkEnabled =false; 
boolean canGetLocation =false; 

Location location; 
protected LocationManager locationManager; 



    public GPSTracker(Context context){ 
    this.context=context; 
    } 

// create a GetLocation Method // 

public Location getLocation(){ 

    try{ 
     locationManager=(LocationManager) context.getSystemService(LOCATION_SERVICE); 
     isGPSEnabled = locationManager.isProviderEnabled(locationManager.GPS_PROVIDER); 
     isNetworkEnabled=locationManager.isProviderEnabled(locationManager.NETWORK_PROVIDER); 

     if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
     || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){ 


      if(isGPSEnabled){ 
       if(location==null){ 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,100000,10,this); 
        if(locationManager!=null){ 
          location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
        } 
        } 
      } 
       // if location is not found from GPS then it will found from network // 
      if(location==null){ 
       if(isNetworkEnabled){ 
          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,100000,10,this); 
         if(locationManager!=null){ 
           location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         } 
        } 

      } 
     } 

    }catch(Exception e){ 

    } 
    return location; 
} 




@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onLocationChanged(Location location) { 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 
} 

と私の主な活動コード:

package com.example.nefissa.androidtest; 

import android.location.Location; 
import android.support.v4.app.FragmentActivity; 
    import android.os.Bundle; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.OnMapReadyCallback; 
    import com.google.android.gms.maps.SupportMapFragment; 
    import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements  OnMapReadyCallback { 

private GoogleMap mMap; 
private GPSTracker gpsTracker; 
private Location mLocation; 
double latitude; 
double longitude; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 

    gpsTracker= new GPSTracker(getApplicationContext()); 
    mLocation=gpsTracker.getLocation(); 
    latitude= mLocation.getLatitude(); 
    longitude= mLocation.getLongitude(); 


    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 


/** 
* Manipulates the map once available. 
* This callback is triggered when the map is ready to be used. 
* This is where we can add markers or lines, add listeners or move the camera. In this case, 
* we just add a marker near Sydney, Australia. 
* If Google Play services is not installed on the device, the user will be prompted to install 
* it inside the SupportMapFragment. This method will only be triggered once the user has 
* installed Google Play services and returned to the app. 
*/ 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Sydney and move the camera 
    LatLng sydney = new LatLng(latitude,longitude); 
    mMap.addMarker(new MarkerOptions().position(sydney).title("you are here")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 
012 pleaaaseくれ この私のGPSTRACKER Javaクラスのコードを助けます
+1

を使用してみてくださいa GPSTrackerのサンプルコードには怪しげで欠陥のあるものがあります。代わりにそれを使用して、いくつかの公式のAndroidのドキュメントを読んだり、StackOverflowの[Documentation section](http://stackoverflow.com/documentation/android/1837/location#t=201703291427260931037)を見てください。 –

+0

私はアンドロイドの開発に慣れています。私は学校のプロジェクトに取り組んでいます。ネット上でこの例が見つかりました。ユーザーの現在地を取得しようとしているので、助けてくれますか? –

+0

いくつかのデバッグを使用してください。コントロールフローとサービスとしてサービスを使用していますが、通常のクラスとしてサービスを使用しています。 –

答えて

0

onMapReadyが呼び出された後、gpsTrackerを初期化することをお勧めします。

gpsTracker= new GPSTracker(getApplicationContext()); 

    if(gpsTracker!=null){ 
     mLocation=gpsTracker.getLocation(); 
     latitude= mLocation.getLatitude(); 
     longitude= mLocation.getLongitude(); 
    } 

そして、私はgpsTracker.getLocationをロジックに変更する必要があると思います。 android gps guideをお読みください。

+0

まだ動作しません、私はちょうどあなたが何をすべきだと思いますユーザーの場所を取得しようとしていますか? –

-1

はあなたの問題がある

GPSトラッカー

public GPSTracker(Activity activity) { 
    this.mActivity = activity; 
    getLocation(); 
} 

public void getLocation() { 
    try { 
     locationManager = (LocationManager) mActivity 
       .getSystemService(LOCATION_SERVICE); 

     // getting GPS status 
     isGPSEnabled = locationManager 
       .isProviderEnabled(LocationManager.GPS_PROVIDER); 

     // getting network status 
     isNetworkEnabled = locationManager 
       .isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

     if (!isGPSEnabled || !isNetworkEnabled) { 
      showSettingsAlert(); 
     } else { 
      this.canGetLocation = true; 
      if (isNetworkEnabled) { 
       if (ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
        ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1); 
       } else { 
        locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 
         location = locationManager 
           .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (location != null) { 
          latitude = location.getLatitude(); 
          longitude = location.getLongitude(); 
         } 
        } 
       } 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 




/** 
* Function to get latitude 
*/ 
public double getLatitude() { 
    if (location != null) { 
     latitude = location.getLatitude(); 
    } 

    // return latitude 
    return latitude; 
} 

/** 
* Function to get longitude 
*/ 
public double getLongitude() { 
    if (location != null) { 
     longitude = location.getLongitude(); 
    } 

    // return longitude 
    return longitude; 
} 


/** 
* Function to ic_check GPS/wifi enabled 
* 
* @return boolean 
*/ 
public boolean canGetLocation() { 
    return this.canGetLocation; 
} 

も活動コールからのよう

のonCreate

// 
gps = new GPSTracker(SignUpActivity.this); 
     if (gps.canGetLocation) { 
      lat = String.valueOf(gps.getLatitude()); 
      lng = String.valueOf(gps.getLongitude()); 
     } 

以下のコメントあなたはまだ得たかの問題

+0

何がエラーでしたか?まだ問題なしでそれを使用しています – g7pro

関連する問題