gpt4 book ai didi

java - 使用 Mapbox 时尝试在空对象引用上调用虚拟方法 'double android.location.Location.getLatitude()'

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:30 24 4
gpt4 key购买 nike

当我使用 Mapbox 时,我遇到了与 GPS 相关的问题。

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback,PermissionsListener {

FirebaseAuth auth;

private MapView mapView;
private MapboxMap mapboxMap;
private PermissionsManager permissionsManager;
Location originLocation;
LocationEngine locationEngine;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, "pk.eyJ1Ijoibml0eWFhcm9yYTcyIiwiYSI6ImNqcHpqeDdjYjAwenU0OG82MDBuZWxtdGIifQ.mMPtHap9shSb-WWzZFyPew");
setContentView(R.layout.activity_main);

mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);

/*CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(25.3254275, 83.0088737))
.zoom(14)
.tilt(20)
.build();*/

auth = FirebaseAuth.getInstance();

mapView.getMapAsync(this);
/*@Override
public void onMapReady(MapboxMap mapboxMap) {

mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(25.3254275,83.0088737))
.title("My Location")
.snippet("bro"));


mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(position), 200);*/


}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);

}

@Override
@SuppressWarnings({"MissingPermission"})
public void onStart() {
super.onStart();
mapView.onStart();

}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {

Toast.makeText(this, "Explanation needed", Toast.LENGTH_LONG).show();

}

@Override
public void onPermissionResult(boolean granted) {

if (granted) {
initializeLocationEngine();
enableLocationComponent();
} else {
Toast.makeText(this, "Permission Needed", Toast.LENGTH_LONG).show();
finish();
}

}

@Override
public void onMapReady(MapboxMap mapboxMap) {
MainActivity.this.mapboxMap = mapboxMap;
locationEnable();

}

void locationEnable() {
if (PermissionsManager.areLocationPermissionsGranted(this)) {
initializeLocationEngine();
enableLocationComponent();
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}


@SuppressWarnings({"MissingPermission"})
private void enableLocationComponent() {

if (PermissionsManager.areLocationPermissionsGranted(this)) {


Location lastLocation = locationEngine.getLastLocation();

FirebaseUser firebaseUser = auth.getCurrentUser();
String userId = firebaseUser.getUid();

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(userId);

reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

HashMap user = (HashMap) dataSnapshot.getValue();
Log.i("userid", user.get("userId").toString());
Log.i("username", user.get("username").toString());

reference.child("latLng").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

Log.i("userblaa", dataSnapshot.toString());
if(lastLocation!=null){
originLocation=lastLocation;
}
Log.i("Location",String.valueOf(lastLocation.getLatitude())+lastLocation.getLongitude());


LatLng latLng = new LatLng(originLocation.getLatitude(), originLocation.getLongitude());

IconFactory iconFactory = IconFactory.getInstance(MainActivity.this);
Bitmap iconDrawable = BitmapFactory.decodeResource(getResources(),R.drawable.marker);
iconDrawable=Bitmap.createScaledBitmap(iconDrawable, 70, 70, true);
com.mapbox.mapboxsdk.annotations.Icon icon=iconFactory.fromBitmap(iconDrawable);

mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(originLocation.getLatitude(), originLocation.getLongitude()))
.setIcon(icon)
);

mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(originLocation.getLatitude(), originLocation.getLongitude()), 12));

reference.child("latLng").setValue(latLng);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
//Log.i("userlatlng",user.getLatLng().getLatitude()+" "+user.getLatLng().getLongitude());

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

这是错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nitya.starein, PID: 8819 java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at com.example.nitya.starein.MainActivity$1$1.onDataChange(MainActivity.java:210) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

似乎“originLocation”没有用“lastLocation”初始化,可能发生这种情况是因为“lastLocation”也为空。

关于java - 使用 Mapbox 时尝试在空对象引用上调用虚拟方法 'double android.location.Location.getLatitude()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56138380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com