2016-04-27 130 views
1

我正尝试使用Google Play服务创建Android位置API。 但我一直在得到“java.lang.IllegalStateException:需要GoogleApiClient”。 谁能告诉我我做错了什么? 这是我一直在尝试的代码。致命异常:java.lang.IllegalArgumentException:需要GoogleApiClient参数

CODE:

package com.example.jamshi.locationapi; 

import android.app.Activity; 
import android.location.Location; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

public class MainActivity extends Activity implements ConnectionCallbacks,OnConnectionFailedListener { 

    private static final String TAG = MainActivity.class.getSimpleName(); 
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 1000; 
    private Location mLastLocation; 
    private GoogleApiClient mGoogleApiClient; 


    private TextView tvLocation; 
    private Button btnShowLocation,btnLocationUpdates; 


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

     tvLocation = (TextView) findViewById(R.id.tvLocation); 
     btnShowLocation = (Button) findViewById(R.id.btnShowLocation); 
     btnLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates); 

     boolean checkPlayServices = false; 
     if(checkPlayServices){ 
      buildApiClient(); 
     } 

     btnShowLocation.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       displayLocation(); 
      } 
     }); 



    } 

    private void displayLocation() { 
     //buildApiClient(); 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

     if (mLastLocation != null) { 
      double latitude = mLastLocation.getLatitude(); 
      double longitude = mLastLocation.getLongitude(); 
      tvLocation.setText(latitude + ", " + longitude); 

     } else { 

      tvLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)"); 
     } 
    } 

    private boolean checkPlayServices(){ 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if(resultCode != ConnectionResult.SUCCESS){ 
      if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){ 
       GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
      }else{ 
       Toast.makeText(getApplicationContext(),"This device is not supported",Toast.LENGTH_LONG).show(); 
       finish(); 
      } 
      return false; 
     } 
     return true; 
    } 

    protected synchronized void buildApiClient() { 
     if (mGoogleApiClient == null) { 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .addApi(LocationServices.API).build(); 
     } 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     if (mGoogleApiClient != null) { 
      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     checkPlayServices(); 

    } 






    /** 
    * Google api callback methods 
    */ 
    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " 
       + result.getErrorCode()); 
    } 

    @Override 
    public void onConnected(Bundle arg0) { 

     // Once connected with google api, get the location 
     displayLocation(); 

    } 

    @Override 
    public void onConnectionSuspended(int arg0) { 
     mGoogleApiClient.connect(); 
    } 


} 

回答

0

因为mGoogleApiClient为空。

buildApiClient()方法永远不会又称,可能是你在的onCreate

boolean checkPlayServices = false; 
if(checkPlayServices){ 
    buildApiClient(); 
} 

犯了一个错误阐述

这checkPlayServices变量这里假的,所以它不会调用buildApiClient()方法,因此mGoogleApiClient为空。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ... 
    boolean checkPlayServices = false; 
    if(checkPlayServices){ 
     buildApiClient(); 
    } 
} 

连接方法不叫,因为mGoogleApiClient是空

@Override 
protected void onStart() { 
    super.onStart(); 
    if (mGoogleApiClient != null) { 
     mGoogleApiClient.connect(); 
    } 
} 

活动创建后,当你调用displayLocation()方法,它崩溃由于mGoogleApiClient为空

private void displayLocation() { 
    //buildApiClient(); 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    ... 
} 
+0

请你详细说明一下吗? – user6239257

+0

编辑答案 – Blackkara

+0

我明白了这个错误,并且从代码中删除了“boolean checkPlayServices = false;”............................ ......但编译时发生错误,指出“无法解析符号”checkPlayServices“”..........................可以请你纠正错误.................................. – user6239257

相关问题