2017-01-20 4 views
0

Ich versuche Ort des virtuellen Geräts AVD das Problem hier zu bekommen, dass die Lage Rückkehr von null, ich habe die Erlaubnis zu der App und noch Standort zu erhalten scheiterteAndroid Fails bekommen Lage

public class MainActivity extends AppCompatActivity implements LocationListener { 
LocationManager locationManager; 
String provider; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    provider = locationManager.getBestProvider(new Criteria(), false); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     //public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //          int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 

     Log.i("Location Info","Permission Denied"); 
     return; 
    } 
    Location location = locationManager.getLastKnownLocation(provider); 
    if (location != null) { 
     Log.i("Location Info","Location Achieved "); 

    } else { 

     Log.i("Location Info", "Location Failed"); 
    } 

} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    locationManager.requestLocationUpdates(provider, 400, 1, this); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    locationManager.removeUpdates(this); 
} 

@Override 
public void onLocationChanged(Location location) { 
     Double longitude = location.getLongitude(); 
     Double latitude = location.getLatitude(); 
     Log.i("location Info Lat",longitude.toString()); 
     Log.i("Location Info long",longitude.toString()); 
} 

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

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

Das Ergebnis ist die ganze Zeit Log-Info: Location Failed, was ist die Lösung in diesem Fall

Antwort

0

Location ist null auf Emulator, weil es nicht wissen, Ihre zuletzt gesendete Position (Neubeginn oder etwas anderes). Sie müssen Ihre Koordinaten an den Emulator senden. Funktioniert besser, wenn Sie auf einem echten Gerät testen.

Edit:

Geben Sie Koordinaten wie in diesem image.

+0

Danke, Sie helfen mir wirklich eine Meile :) –

+0

Sie können die Antwort als akzeptiert markieren, wenn Sie wollen: D –

0

Das Problem hier war wie von @Florescu erwähnt es beginnt von unbekannten Zustand, wenn ich dem Emulator die Dimension zum ersten Mal geben, finden Sie heraus, dass der Emulator dies als eine Bewegung verstehen, aber wenn ich es wieder laufen gibt es mir die richtige Verhalten

Verwandte Themen