Java 安卓工作室“;“类”;或;接口“;预期

Java 安卓工作室“;“类”;或;接口“;预期,java,android,android-studio,Java,Android,Android Studio,我按照中提供的说明显示用户位置。但导致上述编译错误 这是我的密码 public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private GoogleApiClient mGoogleApiClient; private Location mLas

我按照中提供的说明显示用户位置。但导致上述编译错误

这是我的密码

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) {

    }
}

我仔细检查了这个错误,发现它是一个语法错误。谁能告诉我编译失败的原因吗?

您的
buildGoogleAppClient
方法不能在
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); } // 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)中定义一个函数(buildGoogleAppClient),这在Java中是不可能的

protected void onCreate(Bundle savedInstanceState) { 
    //
    // Body of this function
    //
}
因此,基本上在Java中,花括号标记代码块的边界。代码块可以是if块,而块或功能块等。Java不允许在功能块内使用功能块。只有类块可以包含功能块

因此,您需要直接在类块下定义函数

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();

   }

}

我很感激你的回答。但是你有没有通过我在开始时提供的链接。?在这里,他们明确提到“在活动的onCreate()方法中,使用GoogleAppClient.Builder创建一个Google API客户端实例。使用该生成器添加LocationServices API。”@h.APP.y我没有浏览链接。如果需要在onCreate中创建GoogleAPI客户端实例,只需将该语句直接放在onCreate中即可。不要将它放在另一个方法中。对于第一个示例,您不需要在onCreate()中调用实际的buildGoogleAppClient()方法来创建Google API客户端实例吗?@CommittedRoider这是另一个选项。我的第一个示例仅建议如何修复编译错误。我最初并不知道onCreate必须调用该方法。不过这是个好主意!
protected void onCreate(Bundle savedInstanceState) { 
    //
    // Body of this function
    //
}
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();

   }

}