2015-01-20 3 views
5

Ich bin folgende Anweisung in http://developer.android.com/training/location/retrieve-current.html zur Verfügung, um Benutzerstandort anzuzeigen. Dies führt jedoch zu dem oben genannten Kompilierungsfehler.Android Studio "Klasse" oder "Schnittstelle" erwartet

dies ist mein 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) { 

    } 
} 

ich durch den Fehler gehen und fand seinen einen Syntaxfehler. Kann mir jemand sagen, warum es bei der Zusammenstellung versagt?

Antwort

3

Ihre buildGoogleApiClient Methode kann nicht in Ihrer onCreate Methode enthalten sein.

Ändern Sie es an:

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

Oder:

@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

Ich schätze Ihre Antwort @Eran. Aber bist du durch den Link gegangen, den ich am Anfang gestellt habe? Es wurden sie deutlich erwähnt ** "In Ihrer Tätigkeit des onCreate() -Methode, eine Instanz von Google-API-Client mit GoogleApiClient.Builder erstellen. Verwenden Sie den Builder, um die LocationServices API hinzuzufügen." ** – Aparichith

+0

@ h.APP.y I ging nicht durch den Link. Wenn Sie die Instanz von Google API Client in Ihrem onCreate erstellen müssen, fügen Sie diese Anweisung direkt in onCreate ein. Legen Sie es nicht in eine andere Methode. – Eran

+0

Für Ihr erstes Beispiel müssen Sie innerhalb von onCreate() nicht die tatsächliche buildGoogleApiClient() -Methode aufrufen, um die Instanz des Google API-Clients zu erstellen? – 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 

werden sollten:

@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 

Ihre Syntax war weg wegen fehl am Platz geschweiften Klammern. Pass auf die kleinen Dinge auf.

1

Das Problem mit Ihrem Code ist, dass Sie versuchen, eine Funktion (buildGoogleApiClient) in einer anderen Funktion (onCreate) zu definieren, die mit Java nicht möglich ist.

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

Also im Grunde in Java markieren geschweifte Klammern die Grenzen eines Codeblocks. Ein Codeblock kann ein if-Block, While-Block oder Funktionsblock usw. sein. Java erlaubt keinen Funktionsblock innerhalb eines Funktionsblocks. Nur Klassenblöcke können einen Funktionsblock enthalten.

Also müssen Sie Ihre Funktionen direkt unter dem Klassenblock definieren.

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

    } 

} 
Verwandte Themen