- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的遇到了这个错误,已经尝试了我能想到的一切来避免 getLatitude 方法上的 NPE...
有人可以检查我的代码并推荐解决方案吗?
只有当我在纬度和经度的某些坐标中进行硬编码时,它才有效,但这不是我想要做的。提前致谢。
CODE
public class CoPilotMapActivity extends FragmentActivity implements OnMapReadyCallback {
private static final int REQUEST_CHECK_SETTINGS = 0x1;
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
private static final int REQUEST_FINE_LOCATION = 1;
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
private static final long FASTEST_INTERVAL = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
private static final String TAG = "";
private static final long MIN_TIME_BW_UPDATES = 1;
private static final float MIN_DISTANCE_CHANGE_FOR_UPDATES = 1;
private boolean mRequestingLocationUpdates;
private GoogleMap mMap;
private GeoFire mGeoFire;
private LocationRequest locationRequest;
private LocationCallback locationCallback;
private Marker currentUser;
private DatabaseReference myLocationRef;
private GeoFire geoFire;
private List<LatLng> dangerousArea;
private LocationSettingsRequest mLocationSettingsRequest;
private SettingsClient mSettingsClient;
private FusedLocationProviderClient mFusedLocationClient;
private Button mStartUpdatesButton;
private Button mLogout;
private Button mRequest;
private TextView mLatitudeTextView;
private TextView mLongitudeTextView;
private TextView mLastUpdateTimeTextView;
private String mLatitudeLabel;
private String mLongitudeLabel;
private String mLastUpdateTimeLabel;
private String mLastUpdateTime;
private LocationCallback mLocationCallback;
private Location mCurrentLocation;
private LatLng mPickupLocationLatLng;
private LocationManager mLocationManager;
final CoPilotMapActivity mContext = this;
Location mLastLocation;
LocationRequest mLocationRequest;
Location location; // location
double latitude; // latitude
double longitude; // longitude
private LocationManager locationManager;
private double mLatitude;
private double Longitude;
private boolean isGPSEnabled;
private boolean isNetworkEnabled;
private boolean canGetLocation;
private Object markerDestination;
public CoPilotMapActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_co_pilot_map);
// 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);
//Signout from firebase after declaring button up top
mLogout = (Button) findViewById(R.id.logout);
//Call an uber driver
mRequest = (Button) findViewById(R.id.request);
mLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(CoPilotMapActivity.this, "Sign out successful", Toast.LENGTH_SHORT).show();
FirebaseAuth.getInstance().signOut();
//To take you to a different page you create an intent
Intent intent = new Intent(CoPilotMapActivity.this, MainActivity.class);
startActivity(intent);
finish();
return;
}
});
//ACCESS USER'S LOCATION.................................
Dexter.withActivity(this)
.withPermission(Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse response) {
//Obtain the SupportMapFragment and get notified when map is ready to be used
buildLocationRequest();
buildLocationCallback();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(CoPilotMapActivity.this);
SupportMapFragment mapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(CoPilotMapActivity.this);
}
@Override
public void onPermissionDenied(PermissionDeniedResponse response) {
Toast.makeText(CoPilotMapActivity.this,"You must enable permission", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
}
}).check();
//requesting a driver
//WORK ON THIS PART NULL POINTER EXCEPTION COMING UP ON GETLATITUDE FUNCTION
mRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(location!= null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
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()),
new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
if(location!= null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
if (error != null){
System.err.println("There was an error saving the location to GeoFire: " + error);
}else {
System.out.println("Location saved on server successfully!");
}
}
});
geoFire.getLocation(userId, new com.firebase.geofire.LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
if (location != null) {
System.out.println(String.format("The location for key %s is [%f,%f]", key, location.latitude,
location.longitude));
}else {
System.out.println(String.format("There is no location for key %s in GeoFire", key));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//THIS STILL ISN'T BLOODY WORKING RETRIEVING DRIVERS LOCATION WHEN BUTTON IS CALLED SO
//WHAT TO DO DAMMIT
//create marker for customers pickup spot
mPickupLocationLatLng = new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude());
//change text of request button when it is pressed to request driver
mRequest.setText("Getting your driver...");
}
});
}
LOGCAT
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.e.friendrides, PID: 18776
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.e.friendrides.CoPilotMapActivity$3.onClick(CoPilotMapActivity.java:214)
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)
最佳答案
这就是如何不通过“getLatitude”方法获取 NPE
代码
//requesting a driver
//WORK ON THIS PART NULL POINTER EXCEPTION COMING UP ON GETLATITUDE FUNCTION
mRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//check if gps is enabled or not AND TURN IT ON IF IT'S NOT
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//Write Function to enable the gps
OnGPS();
}else{
//If GPS IS ALREADY ON then ...
getLocation();
}
}
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);
if (mLastLocation != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
geoFire.setLocation(userId, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));
}
//THIS STILL ISN'T BLOODY WORKING RETRIEVING DRIVERS LOCATION WHEN BUTTON IS CALLED SO
//WHAT TO DO DAMMIT
//create marker for customers pickup spot
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
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...");
}
}
});
}
关于java - 调用虚方法 double android.location.Location.getLatitude,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257056/
我正在尝试在 python 中编写正则表达式来查找目录路径:我的文本如下所示: text = "The public disk is: \\\\diskA\\FolderB\\SubFolderC\\
我想写一个LocationListener,它把最近最精确的位置作为它的位置。我打算在我的 LocationListener 中使用此代码: @Override public void
我想建立一个有光泽和 plotly 的交互式图表。 Shiny 有一个内置功能来获取有关用户交互的信息。比如:input$plot_click、input$plot_dblclick、input$pl
我正在使用 MobileFirst 提供的 WL.Device.Geo.acquirePosition(onGeoLocationSuccess, onGeoLocationFailure, opti
我最近开始使用 ionic 框架,它里面有 angular js。为了在屏幕之间导航,我使用了 $location.path 并且效果很好。但是,在我下载的一个示例中,我看到 $state.go 被用
谁能解释一下这种行为?我使用 history.pushState(null, null, '#test'); 推送历史状态。然后,当尝试使用console.log(window.location.ha
这里是相关代码: https://www.facebook.com/sharer/sharer.php?u={{$location.absUrl()}} https://www.facebook.c
这两个重定向之间有什么区别?我有一个应用程序,当我使用时,它可以在 chrome 和 android 4 上正常工作,但在 android 2.x.x 上不能正常工作 document.locatio
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
有什么区别 window.location.href="http://example.com"; window.location.replace("http://example.com"); wind
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
以下 3 个指令之间有区别吗? location ~* \.(png)$ { expires max; log_not_found off; } location ~ \.(png)$ {
位于正文末尾之前的以下脚本在 Internet Explorer 和 Chrome(以及任何其他浏览器)中都会被调用。但重定向到指定的 URL 仅发生在 IE 中。我还尝试了 window.locat
我正在使用 Angular ngRouter。我需要更改 url 路径以及搜索参数。我知道 $location.path 和 $location.search,但是有没有一个函数可以同时设置它们? 最
在angularjs中用$location和window.location哪个更好。 例如,我们可以使用$location.path() 或window.location.href 来完成同样的工作。
我在我的网站上使用上述 2 个命令。似乎它们对 95% 访问它应该触发的页面的人有效,但对其他人则不会。 有谁知道是否可以完全阻止这些 javascript 命令?我真的很头疼为什么它们有时不起作用。
这是我无法弄清楚的另一个错误。 我有这个类ExtendedLocation extends Location实例化时抛出 ClassCastExceptioncurrentGpsLocation =
我一直在尝试简单地将一个包含两个变量(一个字符串和一个位置)的类推送和读取到 firebase,但我一直收到此错误。 **com.google.firebase.database.DatabaseEx
我注意到 iPhone 上的“常用位置”似乎比监控 iOS 访问的应用程序 (https://developer.apple.com/reference/corelocation/clvisit) 使
在我的 javascript 代码中,在某些时候,我需要刷新窗口(用户上传了新图片但在页面中仍然可以看到它) 我想知道为什么 location.href = location.href 不刷新窗口?
我是一名优秀的程序员,十分优秀!