Android Studio 预计 "class" 或 "interface"
Android Studio "class" or "interface" expected
我正在按照 http://developer.android.com/training/location/retrieve-current.html 中提供的说明显示用户位置。但导致上述编译错误。
这是我的代码
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) {
}
}
我检查了这个错误,发现它是一个语法错误。谁能告诉我编译失败的原因?
您的 buildGoogleApiClient
方法不能在您的 onCreate
方法中。
将其更改为:
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 ();
}
或:
@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
应该是:
@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
由于花括号放错位置,您的语法错误。注意小事。
您的代码存在的问题是您试图在另一个函数 ( onCreate ) 中定义一个函数 ( buildGoogleApiClient ),这在 Java.
中是不可能的
protected void onCreate(Bundle savedInstanceState) {
//
// Body of this function
//
}
所以基本上在 Java 中,花括号标记了代码块的边界。代码块可以是 if 块、while 块或功能块等。Java 不允许功能块内部有功能块。只有 class-块可以包含功能块。
因此,您需要直接在 class 块下定义函数。
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();
}
}
我正在按照 http://developer.android.com/training/location/retrieve-current.html 中提供的说明显示用户位置。但导致上述编译错误。
这是我的代码
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) {
}
}
我检查了这个错误,发现它是一个语法错误。谁能告诉我编译失败的原因?
您的 buildGoogleApiClient
方法不能在您的 onCreate
方法中。
将其更改为:
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 ();
}
或:
@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
应该是:
@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
由于花括号放错位置,您的语法错误。注意小事。
您的代码存在的问题是您试图在另一个函数 ( onCreate ) 中定义一个函数 ( buildGoogleApiClient ),这在 Java.
中是不可能的protected void onCreate(Bundle savedInstanceState) {
//
// Body of this function
//
}
所以基本上在 Java 中,花括号标记了代码块的边界。代码块可以是 if 块、while 块或功能块等。Java 不允许功能块内部有功能块。只有 class-块可以包含功能块。
因此,您需要直接在 class 块下定义函数。
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();
}
}