i am following instruction provide in http://developer.android.com/training/location/retrieve-current.html to display user location. But leading to above stated compilation error.
this is my code
public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
TextView tv1 = (TextView) findViewById(R.id.lat);
TextView tv2 = (TextView) findViewById(R.id.lon);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //ERROR '}' expected
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
} // ERROR 'class' or 'interface' expected
@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);
}
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
tv1.setText(String.valueOf(mLastLocation.getLatitude()));
tv1.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
I go through the error and found its a syntax error. Can anyone tell me why it is failing in compilation?
Your buildGoogleApiClient
method can't be inside your onCreate
method.
Change it to :
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGoogleApiClient ();
}
Or :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //ERROR '}' expected
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
} // ERROR 'class' or 'interface' expected
Should be:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); } // curly brace to close method and clean up error
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
// removing this curly brace should clear up the error here
Your syntax was off because of misplaced curly braces. Watch out for the little things.
The problem with your code is that you are trying to define a function ( buildGoogleApiClient ) inside another function ( onCreate ) which is not possible with Java.
protected void onCreate(Bundle savedInstanceState) {
//
// Body of this function
//
}
So basically in Java curly braces mark the boundaries of a code block. A code block can be a if-block, while-block or function-block etc. Java does not allow a function-block inside a function-block. Only class-block can contain a function-block.
So, you need to define your functions directly under the class-block.
public class Blah extends Activity implements BlahInterface {
private BlahApiClient mBlahApiClient;
protected synchronized void buildBlahApiClient() {
mBlahApiClient = new BlahApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
protected void onCreate( Bundel savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// You can call (execute) any defined function inside this function
buildBlahApiClient();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With