2014-05-15 2 views
1

그래서 Google지도 V2를 사용하여 모바일 앱을 제작하는 Lynda.com 자습서를 수행 중이며지도를 작성하는 부분과 함께 진행했습니다. 응용 프로그램은 setMyLocationEnabled(true) 방법에 의존하기보다는 프로그램 방식으로 위치를 찾는 것입니다. 그래서 내가 코드와 나는 그것이 충돌 응용 프로그램을 실행하고 로그 캣 나에게 다음과 같은 오류를 제공하기 위해 갈 때마다 함께 따랐습니다 : 여기LocationClient (MainActivity, MainActivity, MainActivity) 생성자가 정의되지 않았습니다.

05-15 12:18:35.139: E/AndroidRuntime(22270): FATAL EXCEPTION: main 
05-15 12:18:35.139: E/AndroidRuntime(22270): Process: com.example.gmapsapp, PID: 22270 
05-15 12:18:35.139: E/AndroidRuntime(22270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gmapsapp/com.example.gmapsapp.MainActivity}: java.lang.ClassCastException: com.example.gmapsapp.MainActivity cannot be cast to com.google.android.gms.common.GooglePlayServicesClient$ConnectionCallbacks 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.access$900(ActivityThread.java:161) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.os.Handler.dispatchMessage(Handler.java:102) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.os.Looper.loop(Looper.java:157) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.main(ActivityThread.java:5356) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at java.lang.reflect.Method.invokeNative(Native Method) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at java.lang.reflect.Method.invoke(Method.java:515) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at dalvik.system.NativeStart.main(Native Method) 
05-15 12:18:35.139: E/AndroidRuntime(22270): Caused by: java.lang.ClassCastException: com.example.gmapsapp.MainActivity cannot be cast to com.google.android.gms.common.GooglePlayServicesClient$ConnectionCallbacks 
05-15 12:18:35.139: E/AndroidRuntime(22270): at com.example.gmapsapp.MainActivity.onCreate(MainActivity.java:60) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.Activity.performCreate(Activity.java:5426) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
05-15 12:18:35.139: E/AndroidRuntime(22270): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269) 
05-15 12:18:35.139: E/AndroidRuntime(22270): ... 11 more 

내 MainActivity.java의 코드가 있어요, 내가 알고하자 그래서 당신은 이러한 인터페이스를 구현해야합니다

GooglePlayServicesClient.ConnectionCallbacks 
GooglePlayServicesClient.OnConnectionFailedListener 

:

package com.example.gmapsapp; 

import java.io.IOException; 
import java.util.List; 

import android.app.Dialog; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.CameraPosition; 
import com.google.android.gms.maps.model.LatLng; 

public class MainActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{ 

    private static final int GPS_ERRORDIALOG_REQUEST = 9001; 
    @SuppressWarnings("unused") 
    private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9002; 
    GoogleMap mMap; 

    @SuppressWarnings("unused") 
    private static final double SEATTLE_LAT = 47.60621, 
    SEATTLE_LNG =-122.33207, 
    SYDNEY_LAT = -33.867487, 
    SYDNEY_LNG = 151.20699, 
    NEWYORK_LAT = 40.714353, 
    NEWYORK_LNG = -74.005973; 
    private static final float DEFAULTZOOM = 15; 
    @SuppressWarnings("unused") 
    private static final String LOGTAG = "Maps"; 

    LocationClient mLocationClient; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (servicesOK()) { 
      setContentView(R.layout.activity_map); 

      if (initMap()) { 
//    mMap.setMyLocationEnabled(true); 
       mLocationClient = new LocationClient(this, (ConnectionCallbacks) this, this); 
       mLocationClient.connect(); 

      } 
      else { 
       Toast.makeText(this, "Map not available!", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     else { 
      setContentView(R.layout.activity_main); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    public boolean servicesOK() { 
     int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

     if (isAvailable == ConnectionResult.SUCCESS) { 
      return true; 
     } 
     else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) { 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST); 
      dialog.show(); 
     } 
     else { 
      Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show(); 
     } 
     return false; 
    } 

    private boolean initMap() { 
     if (mMap == null) { 
      SupportMapFragment mapFrag = 
        (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
      mMap = mapFrag.getMap(); 
     } 
     return (mMap != null); 
    } 

    @SuppressWarnings("unused") 
    private void gotoLocation(double lat, double lng) { 
     LatLng ll = new LatLng(lat, lng); 
     CameraUpdate update = CameraUpdateFactory.newLatLng(ll); 
     mMap.moveCamera(update); 
    } 

    private void gotoLocation(double lat, double lng, 
      float zoom) { 
     LatLng ll = new LatLng(lat, lng); 
     CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom); 
     mMap.moveCamera(update); 
    } 

    public void geoLocate(View v) throws IOException { 
     hideSoftKeyboard(v); 

     EditText et = (EditText) findViewById(R.id.editText1); 
     String location = et.getText().toString(); 

     Geocoder gc = new Geocoder(this); 
     List<Address> list = gc.getFromLocationName(location, 1); 
     Address add = list.get(0); 
     String locality = add.getLocality(); 
     Toast.makeText(this, locality, Toast.LENGTH_LONG).show(); 

     double lat = add.getLatitude(); 
     double lng = add.getLongitude(); 

     gotoLocation(lat, lng, DEFAULTZOOM); 

    } 

    private void hideSoftKeyboard(View v) { 
     InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
     case R.id.mapTypeNone: 
      mMap.setMapType(GoogleMap.MAP_TYPE_NONE); 
      break; 
     case R.id.mapTypeNormal: 
      mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
      break; 
     case R.id.mapTypeSatellite: 
      mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
      break; 
     case R.id.mapTypeTerrain: 
      mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
      break; 
     case R.id.mapTypeHybrid: 
      mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
      break; 
     case R.id.gotoCurrentLocation: 
      gotoCurrentLocation(); 
      break; 
     default: 
      break; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     MapStateManager mgr = new MapStateManager(this); 
     mgr.saveMapState(mMap); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     MapStateManager mgr = new MapStateManager(this); 
     CameraPosition position = mgr.getSavedCameraPosition(); 
     if (position != null) { 
      CameraUpdate update = CameraUpdateFactory.newCameraPosition(position); 
      mMap.moveCamera(update); 
      //   This is part of the answer to the code challenge 
      mMap.setMapType(mgr.getSavedMapType()); 
     } 
    } 

    protected void gotoCurrentLocation() { 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onConnected(Bundle arg0) { 
     Toast.makeText(this, "Connected to location service", Toast.LENGTH_SHORT).show(); 

    } 

    @Override 
    public void onConnectionSuspended(int arg0) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

생성자에 대한 params가 잘못되었습니다. http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html#LocationClient(android.content.Context, com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks, com .google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener) – Raghunandan

+0

감사합니다. :) – user2196650

답변

1

생성자의 두 번째와 세 번째 매개 변수의 유형이있다 : 당신은 당신이 문제 덕분에 도움을 줄 수 있는지 확인하기 위해 다른 파일이 필요합니다 Activity에서 this을 가리키는 경우 하지만 다른 클래스에서 유사한 인터페이스를 선택했습니다. GoogleApiClient

+0

감사합니다. 나는 이것을 잘 시도해보고 그것이 어떻게 진행되는지 알게합니다! :) – user2196650

+0

감사합니다. @nikis 예, 코드에서 얼굴을 빤히 봤습니다. 도움을 주셔서 감사합니다! :) – user2196650

1

android apis에는 익숙하지 않지만 ConnectionCallbacks (import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks)를 잘못 가져온 것 같습니다. ConnectionCallbacks에 캐스트하면 GoogleApiClient.ConnectionCallbacks 대신 잘못된 가져 오기가 사용됩니다.

편집 됨 : 괜찮음

관련 문제