Reputation: 69
The Below is the MainActivity.Java
file. I have imported all the packages required for LocationService but still I am getting the error
"Cannot find Symbol Variable API"
Below is the line where I am getting the error.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LOCATION_SERVICE.API) //Cannot resolve symbol Variable
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
List of SDK Tools installed along with version.
- Android SDK Build Tools
- Android SDK Tools 24.3.4
- Android Support Repository , rev 19
- Android Support Library, rev 23.0.1
- Google Play Service, rev 26
- Google Repository,rev 21
- Google Play APK expansion library , rev3
- Android Auto API Simulators
Code:
package com.example.android.location2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.location.Location;
import android.widget.TextView;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private final String LOG_TAG ="LaurenceTestApp";
private TextView txtOutput;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a GoogleApiClient instance
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LOCATION_SERVICE.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
txtOutput= (TextView) findViewById(R.id.txtOutput);
}
@Override
protected void onStart() {
super.onStart();
// Connect the client.
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(10); // Update location every second
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(LOG_TAG, "GoogleApiClient connection has been suspend");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(LOG_TAG, "GoogleApiClient connection has failed");
}
@Override
public void onLocationChanged(Location location) {
Log.i(LOG_TAG, location.toString());
//txtOutput.setText(location.toString());
txtOutput.setText(Double.toString(location.getLatitude()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Views: 1765
Reputation: 69
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LOCATION_SERVICE.API) //Cannot resolve symbol Variable
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Instead of Location_Service it should be LocationServices. Changing it resolved the issue.
Thanks everyone for their Help
Upvotes: 1
Reputation: 4037
It seems that you forgot to import the Google Play Services in your app project. That is the reason why your app code and imported package names are not able to find the gms package. The process will be different for both Android Studio and ADT Bundle while using Eclipse. Refer to the official documentation for Setting up Google Play Services.
If you are developing on Android Studio you need to add the dependencies in the build.gradle file of your app project and sync the gradle files. You can refer to the following SO post and look at the answers.
If you are developing on Eclipse using ADT Bundle, things will be slightly different. There is some amount of manual work of copy and pasting of the .jar file into your app directory and import it inside your development envionrment. This way the gms packages would be recognized. Please refer to this step by step document with diagram to achieve this task.
Hope this Helps!!
Upvotes: 0