Been坚持使用了很长时间。不知道要输入哪一行代码,以免出现空指针异常。当我按下Get Uber按钮时,该应用程序不会崩溃,只是没有执行任何操作,黑屏,然后再次显示地图页面。如何做到这一点,以便在我按下按钮时能获得客户的取货地点并更改按钮的文字?
//requesting a driver
mRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
//send request and pinpoint exact location from where customer
//will be picked up from
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userId, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));
//THIS STILL ISN'T BLOODY WORKING RETRIEVING DRIVERS LOCATION WHEN BUTTON IS CALLED
//create marker for customers pickup spot
mPickupLocationLatLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(mPickupLocationLatLng).title("Pickup here"));
//change text of request button when it is pressed to request driver
mRequest.setText("Getting your driver...");
}
});
}
线路原因
geoFire.setLocation(userId,new GeoLocation(mLastLocation.getLatitude(),mLastLocation.getLongitude()));
LOGCAT
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.e.friendrides, PID: 19739
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.e.friendrides.CoPilotMapActivity$2.onClick(CoPilotMapActivity.java:156)
at android.view.View.performClick(View.java:6663)
at android.view.View.performClickInternal(View.java:6635)
at android.view.View.access$3100(View.java:794)
at android.view.View$PerformClick.run(View.java:26199)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)