2016-04-03 178 views
0

我是新來的Android編程和我使用谷歌地方自動填充,但每當我運行該項目,它拋出谷歌API客戶端不connected.My Main_Activity.java文件的錯誤是在谷歌API客戶端沒有連接錯誤

package com.example.sunny.myapplication2; 

/** 
* Created by sunny on 4/3/2016. 
*/ 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.PendingResult; 
import com.google.android.gms.common.api.ResultCallback; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.location.places.PlaceBuffer; 
import com.google.android.gms.location.places.Places; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 


public class enter_your_locality extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener { 
    protected GoogleApiClient mGoogleApiClient; 

    private static final LatLngBounds BOUNDS_INDIA = new LatLngBounds(
      new LatLng(-0, 0), new LatLng(0, 0)); 

    private EditText mAutocompleteView; 
    private RecyclerView mRecyclerView; 
    private LinearLayoutManager mLinearLayoutManager; 
    private PlacesAutoCompleteAdapter mAutoCompleteAdapter; 
    ImageView delete; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .addApi(Places.GEO_DATA_API) 
       .build(); 
     mGoogleApiClient.connect(); 

     setContentView(R.layout.enter_your_locality); 
     mAutocompleteView = (EditText)findViewById(R.id.autocomplete_places); 

     delete=(ImageView)findViewById(R.id.cross); 

     mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(this, R.layout.search_view_adapter, 
       mGoogleApiClient, BOUNDS_INDIA, null); 

     mRecyclerView=(RecyclerView)findViewById(R.id.recyclerView); 
     mLinearLayoutManager=new LinearLayoutManager(this); 
     mRecyclerView.setLayoutManager(mLinearLayoutManager); 
     mRecyclerView.setAdapter(mAutoCompleteAdapter); 
     delete.setOnClickListener(this); 
     mAutocompleteView.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence s, int start, int before, 
             int count) { 
       if (!s.toString().equals("") && mGoogleApiClient.isConnected()) { 
        mAutoCompleteAdapter.getFilter().filter(s.toString()); 
       }else if(!mGoogleApiClient.isConnected()){ 
        Toast.makeText(getApplicationContext(), Constants.API_NOT_CONNECTED,Toast.LENGTH_SHORT).show(); 
        Log.e(Constants.PlacesTag,Constants.API_NOT_CONNECTED); 
       } 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, 
              int after) { 

      } 

      public void afterTextChanged(Editable s) { 

      } 
     }); 
     mRecyclerView.addOnItemTouchListener(
       new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() { 
        @Override 
        public void onItemClick(View view, int position) { 
         final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mAutoCompleteAdapter.getItem(position); 
         final String placeId = String.valueOf(item.placeId); 
         Log.i("TAG", "Autocomplete item selected: " + item.description); 
         /* 
          Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place. 
         */ 

         PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi 
           .getPlaceById(mGoogleApiClient, placeId); 
         placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() { 
          @Override 
          public void onResult(PlaceBuffer places) { 
           if(places.getCount()==1){ 
            //Do the things here on Click..... 
            Toast.makeText(getApplicationContext(),String.valueOf(places.get(0).getLatLng()),Toast.LENGTH_SHORT).show(); 
           }else { 
            Toast.makeText(getApplicationContext(),Constants.SOMETHING_WENT_WRONG,Toast.LENGTH_SHORT).show(); 
           } 
          } 
         }); 
         Log.i("TAG", "Clicked: " + item.description); 
         Log.i("TAG", "Called getPlaceById to get Place details for " + item.placeId); 
        } 
       }) 
     ); 
    } 



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




    @Override 
    public void onConnected(Bundle bundle) { 
     Log.v("Google API Callback", "Connection Done"); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Log.v("Google API Callback", "Connection Suspended"); 
     Log.v("Code", String.valueOf(i)); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.v("Google API Callback","Connection Failed"); 
     Log.v("Error Code", String.valueOf(connectionResult.getErrorCode())); 
     Toast.makeText(this, Constants.API_NOT_CONNECTED,Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onClick(View v) { 
     if(v==delete){ 
      mAutocompleteView.setText(""); 
     } 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()){ 
      Log.v("Google API","Connecting"); 
      mGoogleApiClient.connect(); 
     } 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     if(mGoogleApiClient.isConnected()){ 
      Log.v("Google API","Dis-Connecting"); 
      mGoogleApiClient.disconnect(); 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
    } 
} 

我搜索了相關的文章,但徒勞無益。任何人都可以請指導我。 而我logcat中顯示爲下

04-03 11:32:28.231 11863-11863/? D/dalvikvm: Late-enabling CheckJNI 
04-03 11:32:28.343 11863-11863/com.example.sunny.myapplication2 I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.zze.zzi 
04-03 11:32:28.343 11863-11863/com.example.sunny.myapplication2 W/dalvikvm: VFY: unable to resolve virtual method 553: Landroid/content/pm/PackageManager;.getPackageInstaller()Landroid/content/pm/PackageInstaller; 
04-03 11:32:28.343 11863-11863/com.example.sunny.myapplication2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b 
04-03 11:32:28.407 11863-11863/com.example.sunny.myapplication2 I/GMPM: App measurement is starting up, version: 8487 
04-03 11:32:28.411 11863-11863/com.example.sunny.myapplication2 I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE 
04-03 11:32:28.431 11863-11863/com.example.sunny.myapplication2 E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin. 
04-03 11:32:28.431 11863-11863/com.example.sunny.myapplication2 E/GMPM: Scheduler not set. Not logging error/warn. 
04-03 11:32:28.595 11863-11890/com.example.sunny.myapplication2 E/GMPM: Uploading is not possible. App measurement disabled 
04-03 11:32:28.599 11863-11863/com.example.sunny.myapplication2 W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
04-03 11:32:28.599 11863-11863/com.example.sunny.myapplication2 I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested 
04-03 11:32:28.599 11863-11863/com.example.sunny.myapplication2 W/dalvikvm: VFY: unable to resolve interface method 18896: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
04-03 11:32:28.599 11863-11863/com.example.sunny.myapplication2 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
04-03 11:32:28.599 11863-11863/com.example.sunny.myapplication2 I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode 
04-03 11:32:28.599 11863-11863/com.example.sunny.myapplication2 W/dalvikvm: VFY: unable to resolve interface method 18900: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
04-03 11:32:28.599 11863-11863/com.example.sunny.myapplication2 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
04-03 11:32:28.627 11863-11863/com.example.sunny.myapplication2 D/dalvikvm: GC_FOR_ALLOC freed 326K, 13% free 3022K/3460K, paused 6ms, total 6ms 
04-03 11:32:28.647 11863-11863/com.example.sunny.myapplication2 I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations 
04-03 11:32:28.647 11863-11863/com.example.sunny.myapplication2 W/dalvikvm: VFY: unable to resolve virtual method 616: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
04-03 11:32:28.647 11863-11863/com.example.sunny.myapplication2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
04-03 11:32:28.647 11863-11863/com.example.sunny.myapplication2 I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType 
04-03 11:32:28.667 11863-11863/com.example.sunny.myapplication2 W/dalvikvm: VFY: unable to resolve virtual method 638: Landroid/content/res/TypedArray;.getType (I)I 
04-03 11:32:28.683 11863-11863/com.example.sunny.myapplication2 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
04-03 11:32:28.867 11863-11863/com.example.sunny.myapplication2 D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so 
04-03 11:32:28.891 11863-11863/com.example.sunny.myapplication2 D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
04-03 11:32:28.899 11863-11863/com.example.sunny.myapplication2 D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so 
04-03 11:32:28.951 11863-11863/com.example.sunny.myapplication2 W/EGL_genymotion: eglSurfaceAttrib not implemented 
04-03 11:32:28.951 11863-11863/com.example.sunny.myapplication2 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache 
04-03 11:32:28.951 11863-11863/com.example.sunny.myapplication2 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384 
04-03 11:32:28.971 11863-11863/com.example.sunny.myapplication2 E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 
04-03 11:32:28.971 11863-11863/com.example.sunny.myapplication2 E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384 
04-03 11:32:28.971 11863-11863/com.example.sunny.myapplication2 D/OpenGLRenderer: Enabling debug mode 0 
04-03 11:32:38.731 11863-11890/com.example.sunny.myapplication2 I/GMPM: Tag Manager is not found and thus will not be used 
04-03 11:32:40.175 11863-11883/com.example.sunny.myapplication2 D/dalvikvm: GC_FOR_ALLOC freed 254K, 11% free 3276K/3644K, paused 3ms, total 3ms 
04-03 11:32:40.211 11863-12070/com.example.sunny.myapplication2 W/GooglePlayServicesUtil: Google Play services is missing. 
04-03 11:32:40.311 11863-11863/com.example.sunny.myapplication2 W/EGL_genymotion: eglSurfaceAttrib not implemented 
04-03 11:32:40.315 11863-11863/com.example.sunny.myapplication2 V/Google API Callback: Connection Failed 
04-03 11:32:40.315 11863-11863/com.example.sunny.myapplication2 V/Error Code: 1 
04-03 11:37:39.547 11863-11863/com.example.sunny.myapplication2 D/dalvikvm: GC_FOR_ALLOC freed 282K, 11% free 3504K/3896K, paused 5ms, total 5ms 
04-03 11:37:39.551 11863-11863/com.example.sunny.myapplication2 E/Google Places Auto Complete: Google API not connected 
04-03 11:37:39.979 11863-11863/com.example.sunny.myapplication2 E/Google Places Auto Complete: Google API not connected 
04-03 11:37:40.087 11863-11863/com.example.sunny.myapplication2 E/Google Places Auto Complete: Google API not connected 

回答

0
GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin. 

按照the google guide設置你的項目。

+0

我嘗試了你所建議的方法,但它仍然顯示相同的錯誤 –

相關問題