2016-08-27 2 views
2

안녕하세요, 저는 Google Api 클라이언트의 새 인스턴스를 만들고 '.connect'메서드를 호출하면 GoogleApiClient가 아니라는 Exception을 얻습니다. 아직 연결되어 있지 않습니다.Android Google API 클라이언트가 연결되지 않음

MainActivity.java 것은

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    this.googleApiClient = new GoogleApiClient.Builder(this) 
     .addApi(LocationServices.API) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .build(); 

    //Setup LocationRequest 
    this.locationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(1000 * 10) 
      .setInterval(1000 * 3); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this); 
} 
@Override 
public void onStart(){ 
    super.onStart(); 
    this.googleApiClient.connect(); 
} 

@Override 
public void onStop(){ 
    super.onStop(); 
    this.googleApiClient.disconnect(); 
} 

예외 :

08-27 22:54:04.536 11610-11610/at.ideafactory.spotted E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: at.ideafactory.spotted, PID: 11610 
                    java.lang.RuntimeException: Unable to start activity ComponentInfo{at.ideafactory.spotted/at.ideafactory.spotted.Activity.Main.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet. 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653) 
                     at android.app.ActivityThread.access$800(ActivityThread.java:156) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:157) 
                     at android.app.ActivityThread.main(ActivityThread.java:5872) 
                     at java.lang.reflect.Method.invokeNative(Native Method) 
                     at java.lang.reflect.Method.invoke(Method.java:515) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674) 
                     at dalvik.system.NativeStart.main(Native Method) 
                    Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet. 
                     at com.google.android.gms.internal.zzpy.zzd(Unknown Source) 
                     at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source) 
                     at at.ideafactory.spotted.Activity.Main.MainActivity.onCreate(MainActivity.java:108) 
                     at android.app.Activity.performCreate(Activity.java:5312) 
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)  
                     at android.app.ActivityThread.access$800(ActivityThread.java:156)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:157)  
                     at android.app.ActivityThread.main(ActivityThread.java:5872)  
                     at java.lang.reflect.Method.invokeNative(Native Method)  
                     at java.lang.reflect.Method.invoke(Method.java:515)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)  
                     at dalvik.system.NativeStart.main(Native Method)  

아마 누군가는이 문제를 도와 줄 수 있습니다.

친절 인사말

답변

7

이것은 onStart 안에 연결하려고했기 때문입니다. 다음과 같이 onStart 및 onStop 메서드 내부에 null 체크를 넣으십시오.

@Override 
public void onStart(){ 
    super.onStart(); 
    if(this.googleApiClient != null){ 
      this.googleApiClient.connect(); 
    } 
} 

마찬가지로 onStop()에서 null 확인을하십시오. 다음과 같이

편집

당신은 그것을해야한다. 우선 이들을 귀하의 활동에 구현하십시오. 그리고 OnConnected 콜백 내부에서 다음과 같이하십시오.

public class MainActivity extends AppCompatActivity implements 
    GoogleApiClient.OnConnectionFailedListener, 
    GoogleApiClient.ConnectionCallbacks, 
    LocationListener { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     buildGoogleApiClient(); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     Log.d(TAG, "Connection established. Fetching location .."); 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(1000); 
     mLocationRequest.setFastestInterval(1000); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     Log.d("Lat: " + location.getLatitude() + "Lng : " + location.getLongitude());  
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    } 

    public synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext()) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     //stop location updates when Activity is no longer active 
     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 
    } 
} 
+0

나는 이것을 시도했지만 예외가 다시 발생했습니다. –

+0

코드를 편집 해주세요. @DavidNagl –

+0

@DavidNagl 이걸 실행할 수 있었습니까? –