Reputation: 108
I'm having some trouble and could with some help. I've got a MapsActivity and a LocationHelper class but when loading the MapsActivity I get presented with the errors listed below.
MapsActivity -
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private static final int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 2;
private GoogleMap mMap;
private LocationHelper locationHelper;
private LatLng location;
private boolean onCreateFinished = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onCreateFinished = true;
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
//instantiates LocationHelper for use in this activity
locationHelper = new LocationHelper(this);
//runs startConnection from LocationHelper class
locationHelper.startConnection();
//starts map
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
//sets map markers
public void setMapMarkers(){
//runs checkLocation from LocationHelper class
locationHelper.checkLocation();
location = new LatLng(locationHelper.mLatitude, locationHelper.mLongitude);
LatLng currentLocation = new LatLng(location.latitude, location.longitude);
mMap.addMarker(new MarkerOptions().position(currentLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
}
@Override
protected void onStart() {
locationHelper.mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
if(onCreateFinished == true){
setMapMarkers();
}else{
Toast.makeText(this, "onCreate not finished..", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
locationHelper.mGoogleApiClient.disconnect();
//LocationManager.removeUpdates();
super.onStop();
}
LocationHelper class -
public class LocationHelper extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
//private static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
//private static final int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 2;
GoogleApiClient mGoogleApiClient;
private LocationManager mLocationManager;
//private Location mLastLocation;
private LocationListener mLocationListener;
public double mLatitude;
public double mLongitude;
private Context context;
//constructor
public LocationHelper(Context context) {
//saves the context received from activity that instantiates LocationHelper into a local variable
this.context = context;
}
public void startConnection() {
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
public void checkLocation(){
//gets system service from LocationManager
mLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
//checks permissions
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return;
}
mLocationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
//checks if GPS is enabled
if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
if(mLocationListener != null){
if(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
return;
}
if (mLocationManager != null) {
location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
Toast.makeText(getApplicationContext(), "GPS - " + String.valueOf(mLatitude) + "," + String.valueOf(mLongitude), Toast.LENGTH_SHORT).show();
}
}
}
}
//checks if internet is enabled
else if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
if(mLocationManager != null){
location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
Toast.makeText(getApplicationContext(), "NETWORK - " + String.valueOf(mLatitude) + "," + String.valueOf(mLongitude), Toast.LENGTH_SHORT).show();
}
}
}
//method for setting location request parameters
setLocationRequest();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public void setLocationRequest(){
//sets locationrequest parameters
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(3000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
public double getLatitudeForNote() {
return mLatitude;
}
public double getLongitudeForNote(){
return mLongitude;
}
}
Error -
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a8460p.locationotes, PID: 1715
java.lang.RuntimeException: Unable to resume activity {com.example.a8460p.locationotes/com.example.a8460p.locationotes.MapsActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3160)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3191)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2529)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5300)
at com.example.a8460p.locationotes.LocationHelper.checkLocation(LocationHelper.java:66)
at com.example.a8460p.locationotes.MapsActivity.setMapMarkers(MapsActivity.java:71)
at com.example.a8460p.locationotes.MapsActivity.onResume(MapsActivity.java:88)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1259)
at android.app.Activity.performResume(Activity.java:6361)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3149)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3191)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2529)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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)
Thanks in advance for any help!
Upvotes: 0
Views: 586
Reputation: 281
You need to initialize your LocationHelper instance after onCreate completed. You can move related code into onMapReady() method.
Upvotes: 0
Reputation: 1006934
Step #1: Delete extends Activity
from LocationHelper
. This is not an activity.
Step #2: Use the context
for your call to getSystemService()
inside of LocationHelper
.
Step #3: Decide whether you are going to use Android's LocationManager
or LocationRequest
from the Play Services SDK, and get rid of all the code associated with the one that you are not going to use.
Upvotes: 1