gpt4 book ai didi

java - 出现错误 java.lang.NullPointerException : Attempt to invoke virtual method 'double android.location.Location.getLatitude()'

转载 作者:行者123 更新时间:2023-12-01 17:52:19 25 4
gpt4 key购买 nike

这是来自日志猫的错误消息;

我收到以下错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()”

进程:com.marktech.myrider,PID:20905

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.marktech.myrider.CustomerMapActivity$2.onClick(CustomerMapActivity.java:94)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

这是我的代码

错误就在这一行;

geoFire.setLocation( userId, new GeoLocation( mLastLocation.getLatitude(), mLastLocation.getLongitude() ), new GeoFire.CompletionListener() {

private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
LocationRequest mLocationRequest;

private Button btnLogout, btnRequest;
private LatLng pickupLocation;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_customer_map );
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById( R.id.map );
assert mapFragment != null;
mapFragment.getMapAsync( this );

btnLogout = (Button) findViewById( R.id.btnLogout );
btnRequest = (Button) findViewById( R.id.btnRequest );

btnLogout.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent( CustomerMapActivity.this, MainActivity.class);
startActivity( intent );
finish();
return;

}
} );

btnRequest.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {

if(mLastLocation!= null){
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
}
String userId = FirebaseAuth.getInstance().getUid();

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
GeoFire geoFire = new GeoFire( ref );
geoFire.setLocation( userId, new GeoLocation( mLastLocation.getLatitude(), mLastLocation.getLongitude() ), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {


}
});
pickupLocation = new LatLng( mLastLocation.getLatitude(), mLastLocation.getLongitude() );
mMap.addMarker( new MarkerOptions().position( pickupLocation ).title( "Pickup Here" ) );
btnRequest.setText( "Getting your rider..." );

}
} );


}


@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

if((ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)&&
(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)&&
(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED))
{
return;
}

buildGoogleApiClient();

// mMap.setMyLocationEnabled( true );

}

private synchronized void buildGoogleApiClient(){
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks( this )
.addOnConnectionFailedListener( this )
.addApi( LocationServices.API )
.build();
mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {
mLastLocation = location;

mMap.setMyLocationEnabled( true );
mMap.getUiSettings().setMyLocationButtonEnabled(true);

LatLng latlng = new LatLng( location.getLatitude(),location.getLongitude() );
//moving the camera with the user
mMap.moveCamera( CameraUpdateFactory.newLatLng( latlng ) );
mMap.animateCamera( CameraUpdateFactory.zoomTo( 11 ) );

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval( 1000 );
mLocationRequest.setFastestInterval( 1000 );
mLocationRequest.setPriority( LocationRequest.PRIORITY_HIGH_ACCURACY );

if((ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)&&
(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)&&
(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED))
{
return;
}

if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

//LocationServices.FusedLocationApi.requestLocationUpdates( mGoogleApiClient, mLocationRequest, this );
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
protected void onStop() {
super.onStop();
}

}

最佳答案

mLastLocation 实际上可以为 null。您已经在方法的顶部进行了检查,但没有在...之后进行检查

试试这个:

btnRequest.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View view) {
if (mLastLocation != null) {
double latitude = mLastLocation.getLatitude();
double longitude = mLastLocation.getLongitude();
String userId = FirebaseAuth.getInstance().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userId, new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {@
Override
public void onComplete(String key, DatabaseError error) {}
});
pickupLocation = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions()
.position(pickupLocation)
.title("Pickup Here"));
btnRequest.setText("Getting your rider...");
}
}
});

关于java - 出现错误 java.lang.NullPointerException : Attempt to invoke virtual method 'double android.location.Location.getLatitude()' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60775026/

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