2015-01-20 17 views
5

tôi đang làm theo hướng dẫn cung cấp trong http://developer.android.com/training/location/retrieve-current.html để hiển thị vị trí của người dùng. Nhưng dẫn đến lỗi biên dịch nêu trên.Android Studio "lớp học" hoặc "giao diện" dự kiến ​​

đây là mã của tôi

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

    } 
} 

tôi đi qua các lỗi và tìm thấy nó một lỗi cú pháp. Bất cứ ai có thể cho tôi biết lý do tại sao nó không trong biên dịch?

Trả lời

3

Phương thức buildGoogleApiClient của bạn không thể nằm trong phương thức onCreate của bạn.

Thay đổi nó để:

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

Hoặc:

@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(); 
} 
+0

Tôi đánh giá cao câu trả lời của bạn @Eran. Nhưng bạn đã đi qua liên kết mà tôi đã cung cấp ngay từ đầu. Ở đó, họ đã đề cập rõ ràng ** "Trong phương thức onCreate() của hoạt động của bạn, hãy tạo một phiên bản ứng dụng khách Google API bằng GoogleApiClient.Builder. Sử dụng trình tạo để thêm API LocationServices." ** – Aparichith

+0

@ h.APP.y I đã không đi qua liên kết. Nếu bạn cần tạo cá thể của Ứng dụng khách API của Google trong onCreate của mình, chỉ cần đặt câu lệnh đó trực tiếp trong onCreate. Đừng đặt nó vào một phương pháp khác. – Eran

+0

Ví dụ đầu tiên của bạn, bạn không cần phải gọi phương thức xây dựng thực tếGoogleApiClient() của onCreate() để tạo cá thể của ứng dụng khách Google API? – committedandroider

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

nên là:

@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 

cú pháp của bạn đã tắt vì dấu ngoặc nhọn đặt không đúng chỗ. Xem ra cho những điều nhỏ nhặt.

1

Sự cố với mã của bạn là bạn đang cố gắng xác định hàm (xây dựngGoogleApiClient) bên trong một hàm khác (onCreate) mà không thể thực hiện được với Java.

protected void onCreate(Bundle savedInstanceState) { 
    // 
    // Body of this function 
    // 
} 

Vì vậy, về cơ bản trong dấu ngoặc nhọn Java đánh dấu ranh giới của khối mã. Một khối mã có thể là khối if-block, while-block hoặc function-block, vv Java không cho phép một khối chức năng bên trong khối chức năng. Chỉ khối lớp có thể chứa khối chức năng.

Vì vậy, bạn cần xác định các chức năng của mình ngay dưới khối lớp.

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

    } 

} 
Các vấn đề liên quan