java 应为 Android Studio“类”或“接口”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28039882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio "class" or "interface" expected
提问by Aparichith
i am following instruction provide in http://developer.android.com/training/location/retrieve-current.htmlto display user location. But leading to above stated compilation error.
我正在按照http://developer.android.com/training/location/retrieve-current.html 中提供的说明显示用户位置。但导致上述编译错误。
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?
我检查了错误并发现它是一个语法错误。谁能告诉我为什么编译失败?
采纳答案by Eran
Your buildGoogleApiClient
method can't be inside your onCreate
method.
你的buildGoogleApiClient
方法不能在你的onCreate
方法里面。
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();
}
回答by CreedVI
@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.
由于大括号放错了位置,您的语法已关闭。注意小事。
回答by Sarvesh Kumar Singh
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.
您的代码的问题在于您试图在另一个函数( onCreate )中定义一个函数( buildGoogleApiClient ),这在 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.
所以基本上在 Java 中,花括号标记了代码块的边界。代码块可以是 if 块、while 块或功能块等。Java 不允许功能块内有功能块。只有类块可以包含功能块。
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();
}
}