2015-01-20 17 views
5

Postępuję zgodnie z instrukcjami, podanymi w http://developer.android.com/training/location/retrieve-current.html, aby wyświetlić lokalizację użytkownika. Ale prowadząc do powyższego błędu kompilacji.Android Studio "klasa" lub "interfejs" oczekiwane

to jest mój kod

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

    } 
} 

idę przez błędem i znalazł błąd składni. Czy ktoś może mi powiedzieć, dlaczego nie radzi sobie z kompilacją?

Odpowiedz

3

Twoja metoda buildGoogleApiClient nie może znajdować się w Twojej metodzie onCreate.

go zmienić na:

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

czyli

@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

Doceniam twoją odpowiedź @Eran. Ale czy przeszedłeś link, który podałem na początku? W tym miejscu wyraźnie wspomniano ** "W metodzie onCreate() w swojej działalności utwórz instancję klienta Google API za pomocą GoogleApiClient.Builder. Użyj konstruktora, aby dodać interfejs API LocationServices." ** – Aparichith

+0

@ h.APP.y I nie przeszedł przez link. Jeśli chcesz utworzyć wystąpienie klienta Google API w swoim onCreate, po prostu umieść to oświadczenie bezpośrednio w onCreate. Nie umieszczaj go w innej metodzie. – Eran

+0

Dla twojego pierwszego przykładu, nie musisz wywoływać rzeczywistej metody buildGoogleApiClient() wewnątrz onCreate(), aby utworzyć instancję klienta 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 

Powinno być:

@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 

składni był wyłączony z powodu niewłaściwie klamrami. Uważaj na małe rzeczy.

1

Problem z kodem polega na tym, że próbujesz zdefiniować funkcję (buildGoogleApiClient) w innej funkcji (onCreate), co nie jest możliwe w Javie.

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

W zasadzie w języku Java nawiasy klamrowe wyznaczają granice bloku kodu. Blok kodu może być blokiem if, blokiem while lub blokiem funkcyjnym itp. Java nie pozwala na blok funkcyjny wewnątrz bloku funkcyjnego. Tylko blok klasy może zawierać blok funkcyjny.

Musisz więc zdefiniować swoje funkcje bezpośrednio pod blokiem klasy.

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

    } 

} 
Powiązane problemy