[英]I cannot bind the contacts in my arraylist can anyone tell me what i do wrong?ANDROID
[英]I have an issue displaying the user's current location on Google Maps in my Android app. Can anyone tell me what I need to do with my code
這是我的MapActivity的代碼。 它僅用於跟蹤用戶位置,但僅在移動時才顯示位置,但是當我添加代碼以在應用啟動時顯示當前位置時,它開始崩潰。
import android.*;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
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;
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
if (Build.VERSION.SDK_INT < 23) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
}
}
}
}
問題是,我可以在更改位置並從Emulator發送位置時獲取位置,但是當我添加else部分以顯示當前位置時,應用崩潰了。
這是啟動應用程序時遇到的錯誤。
2019-03-06 01:50:39.000 24094-24094 / com.example.userlocationdemo E / AndroidRuntime:致命例外:主進程:com.example.userlocationdemo,PID:24094 java.lang.RuntimeException:無法啟動活動ComponentInfo { com.example.userlocationdemo / com.example.userlocationdemo.MapsActivity}:嘗試在android.app.ActivityThread上的空對象引用上調用虛擬方法“ double android.location.Location.getLatitude()”。 android.app.ActivityThread.handle上的performLaunchActivity(ActivityThread.java:2678)android.app.ActivityThread $ H.handleMessage(ActivityThread。)上的android.app.ActivityThread.-wrap12(ActivityThread.java)的android.os.Handler.dispatchMessage(Handler.java:102)的java:1490),android.app.ActivityThread.main(ActivityThread.java:6165)的android.os.Looper.loop(Looper.java:154)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(Zygote)上的java.lang.reflect.Method.invoke(本機方法) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)上的Init.java:888)原因:java.lang.NullPointerException:嘗試調用虛擬方法'double android.location.Location.getLatitude() '在com.example.userlocationdemo.MapsActivity.onCreate(MapsActivity.java:94)在android.app.Activity.performCreate(Activity.java:6687)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)上的空對象引用上:1140),位於android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
您正在嘗試從第94行的MapsActivity的oncreate方法中的位置的空對象獲取緯度,如日志打印中所述
com.example.userlocationdemo.MapsActivity.onCreate(MapsActivity.java:94)上的空對象引用上的android.location.Location.getLatitude()'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.