When I run my app, It works perfectly but some time it crashes and following error occurs:
2021-01-06 12:27:58.502 14337-14337/com.example.dashboard1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dashboard1, PID: 14337
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.dashboard1.MainActivity.showLocationTextView(double, double)' on a null object reference
at com.example.dashboard1.LocationService$1.onLocationResult(LocationService.java:110)
at com.google.android.gms.internal.location.zzar.notifyListener(com.google.android.gms:play-services-location@@17.1.0:4)
at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal(com.google.android.gms:play-services-base@@17.3.0:18)
at com.google.android.gms.common.api.internal.ListenerHolder$zaa.handleMessage(com.google.android.gms:play-services-base@@17.3.0:6)
at android.os.Handler.dispatchMessage(Handler.java:102)
at com.google.android.gms.internal.base.zap.dispatchMessage(com.google.android.gms:play-services-base@@17.3.0:8)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
My code of locationService where it happens:
public class LocationService extends android.app.Service{
protected static final String NOTIFICATION_ID = "1337";
private static final String CHANNEL_ID = "riderApp";
private static LocationService mCurrentService;
private int counter = 0;
private NotificationManager notificationManager;
private static final String PACKAGE_NAME = "com.example.dashboard1";
private static final String EXTRA_STARTED_FROM_NOTIFICATION = PACKAGE_NAME + ".started_from_notification";
private LatLng latLng;
private static final String TAG = LocationService.class.getSimpleName();
private FusedLocationProviderClient fusedLocationProviderClient;
private Location_Helper location_helper;
private LocationCallback locationCallback;
private FirebaseFirestore firebaseFirestore;
private FirebaseAuth firebaseAuth;
private ArrayList<LatLng> latLngs = new ArrayList<>();
private float totalD = 0f;
public LocationService() {
}
@Override
public void onCreate() {
super.onCreate();
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
locationCallback = new LocationCallback() {
@SuppressLint("MissingPermission")
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null){
location_helper = new Location_Helper(location.getLongitude(), location.getLatitude());
Double Latitude = location_helper.getLatitude();
Double Longitude = location_helper.getLongitude();
latLng = new LatLng(Latitude, Longitude);
Long timeStamp = System.currentTimeMillis()/1000;
String time = getDate(timeStamp);
Log.d(TAG,"Latitude: "+Latitude+" Longitude: "+Longitude+" Time: "+time);
uploadFirebase(Latitude, Longitude);
MainActivity.getInstance().showLocationTextView(Latitude, Longitude);
if (latLngs == null) {
latLngs = new ArrayList<LatLng>();
}
latLngs.add(new LatLng(Latitude, Longitude));
uploadHistory(latLngs, time);
calculateDistance(time);
if (serviceIsRunningInForeground(LocationService.this)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
notificationManager.notify(1001, getNotification());
}
else {
notificationManager.notify(1002, getNotification());
}
}
}
else {
return;
}
}
};
mCurrentService = this;
}
and main activity code:
public void showLocationTextView(double lat, double lng) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
LatLng latLng = new LatLng(lat, lng);
mgoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));
btnlocation_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "Location: " + latLng);
latLngs.add(latLng);
Long timeStamp = System.currentTimeMillis()/1000;
String time = getDate(timeStamp);
add_Location_Points(latLngs,time);
Toast.makeText(MainActivity.this, "Location added successfully!", Toast.LENGTH_LONG).show();
}
});
}
});
}
I am worry about this. some times this error occurs when my app runs for some time and suddenly this error occurs and app crashes.
Please look at it and tell me the cause of this. I will be very thankful to you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…