2015-01-20 7 views
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

आपकी 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(); 
} 
+0

मैं आपके उत्तर @Eran की सराहना करता हूं। लेकिन क्या आपने लिंक में प्रवेश किया जो मैंने शुरुआत में प्रदान किया था। वहां उन्होंने स्पष्ट रूप से उल्लेख किया है ** "आपकी गतिविधि की ऑन क्रेट() विधि में, GoogleApiClient.builder का उपयोग करके Google API क्लाइंट का एक उदाहरण बनाएं। स्थान सेवा एपीआई जोड़ने के लिए बिल्डर का उपयोग करें।" ** – Aparichith

+0

@ h.APP.y I लिंक के माध्यम से नहीं गए। यदि आपको अपने ऑनक्रेट में Google API क्लाइंट का उदाहरण बनाना है, तो बस उस कथन को सीधे बनाएं पर बनाएं। इसे किसी अन्य विधि के अंदर न रखें। – Eran

+0

अपने पहले उदाहरण के लिए, क्या आपको Google API क्लाइंट का उदाहरण बनाने के लिए onrere() के अंदर वास्तविक buildGoogleApiClient() विधि को कॉल करने की आवश्यकता नहीं है? – 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 

होना चाहिए:

@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

आपके कोड के साथ समस्या यह है कि आप किसी अन्य फ़ंक्शन (ऑनक्रेट) के अंदर एक फ़ंक्शन (buildGoogleApiClient) को परिभाषित करने का प्रयास कर रहे हैं जो जावा के साथ संभव नहीं है।

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

तो मूल रूप से जावा घुंघराले ब्रेसिज़ में कोड ब्लॉक की सीमाओं को चिह्नित किया जाता है। एक कोड ब्लॉक एक if-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(); 

    } 

} 
संबंधित मुद्दे