Android Studio期望“class”或“interface”

5

我正在按照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) {

    }
}

我检查了这个错误,发现它是一个语法错误。有人能告诉我为什么它无法通过编译吗?

3个回答

3

你的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();
}

感谢您的回答@Eran。但是您是否查看了我在开头提供的链接?他们在那里清楚地提到:“在活动的onCreate()方法中,使用GoogleApiClient.Builder创建Google API Client的实例。使用构建器添加LocationServices API。” - Aparichith
@h.APP.y 我没有通过链接。如果您需要在onCreate中创建Google API Client的实例,只需直接将该语句放在onCreate中即可。不要将其放在另一个方法中。 - Eran
对于你的第一个例子,难道不需要在onCreate()方法内调用实际的buildGoogleApiClient()方法来创建Google API客户端的实例吗? - committedandroider
@committedandroider 那是另一个选项。我的第一个示例仅建议如何修复编译错误。我最初并不知道该方法必须由onCreate调用。不过这是个好主意! - Eran

2
@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

您的语法出现了问题,因为花括号放置不当。注意一些小细节。

1
你的代码问题在于,你试图在一个函数(onCreate)内部定义另一个函数(buildGoogleApiClient),这在Java中是不可能的。
protected void onCreate(Bundle savedInstanceState) { 
    //
    // Body of this function
    //
}

基本上,在Java中,花括号标记代码块的边界。 代码块可以是if-block,while-block或function-block等。 Java不允许在function-block内部再定义一个function-block。 只有class-block可以包含function-block。
因此,您需要直接在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();

   }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接