无法使用Google Place API

时间:2015-10-08 08:59:26

标签: java android intellij-idea google-places-api

我正在尝试创建只需连接到Google Place API的小型应用,预期输出就是获取Json数据中的当前位置。

但在此之前,我正面临着这个错误。请帮忙。我尝试了很多东西,似乎没有一个能够工作。如果有人知道最好的学习方法(使用当前的Google Place API),请建议我,因为我厌倦了两周的配置,而不是编写单行代码。

这是相同的代码。 (我正在使用Google Developers指南中的Getting Started代码。)它应该返回可能的位置,但它只会记录错误。

MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MyActivity extends Activity {
      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GooglePlaceCommunicator communicator=new GooglePlaceCommunicator();
        communicator.GetData();
        TextView tv = (TextView) findViewById(R.id.txtView);
        tv.setText("Connection Done");

    }
}

import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GooglePlaceCommunicator communicator=new GooglePlaceCommunicator(); communicator.GetData(); TextView tv = (TextView) findViewById(R.id.txtView); tv.setText("Connection Done"); } }

AndroidManifext.xml (我已经从下面的文件中删除了API密钥,只是在这里发布。)

GooglePlaceCommunicator.java

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.GoogleAPIsLearning" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="16"/> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <activity android:name="MyActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="APIKey" /> </application> </manifest>

import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;
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.places.PlaceLikelihood;
import com.google.android.gms.location.places.PlaceLikelihoodBuffer;
import com.google.android.gms.location.places.Places;


public class GooglePlaceCommunicator extends MultiDexApplication implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

        private GoogleApiClient mGoogleAPIClient;
        public  void GetData(){
            try {
                Log.i("Inside Try","Before GooglePlaceCommunicator");
                mGoogleAPIClient = new GoogleApiClient
                        .Builder(this)
                        .addApi(Places.GEO_DATA_API)
                        .addApi(Places.PLACE_DETECTION_API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .build();

                Log.i("Inside Try","After GooglePlaceCommunicator");

                PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleAPIClient, null);

                result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
                                             @Override
                                             public void onResult(PlaceLikelihoodBuffer placeLikelihoods) {
                                                 for (PlaceLikelihood placeLikelihood : placeLikelihoods) {
                                                     Log.i("TAG", String.format("Place %s has likelihood : %g",
                                                             placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood()));

                                                 }
                                                 placeLikelihoods.release();

                                             }
                                         }
                );
            } catch (Exception e) {
                        Log.i("ERRTBP: ",e.toString());
                }

        }

        @Override
        public void onConnected(Bundle bundle) {
                //super.onStart();
                mGoogleAPIClient.connect();
        }

        @Override
        public void onConnectionSuspended(int i) {
                mGoogleAPIClient.disconnect();
               // super.onStop();

        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {

        }

        @Override
        protected  void attachBaseContext(Context Base) {
                super.attachBaseContext(Base);
                MultiDex.install(this);
        }
}

正如你在上面的课程中看到的,我也改变了AttachBaseContext方法,因为我直接在我的android设备上运行这个应用程序(4.1.2 - API16)

日志包含如下内容。这是我无法解决的错误,尽管浪费了最近2周尝试不同的方法。 正如您所看到的,它位于Communicator类中并记录数据,但最后它只记录空指针异常。 import android.content.Context; import android.os.Bundle; import android.support.multidex.MultiDex; import android.support.multidex.MultiDexApplication; import android.util.Log; 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.places.PlaceLikelihood; import com.google.android.gms.location.places.PlaceLikelihoodBuffer; import com.google.android.gms.location.places.Places; public class GooglePlaceCommunicator extends MultiDexApplication implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { private GoogleApiClient mGoogleAPIClient; public void GetData(){ try { Log.i("Inside Try","Before GooglePlaceCommunicator"); mGoogleAPIClient = new GoogleApiClient .Builder(this) .addApi(Places.GEO_DATA_API) .addApi(Places.PLACE_DETECTION_API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); Log.i("Inside Try","After GooglePlaceCommunicator"); PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleAPIClient, null); result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() { @Override public void onResult(PlaceLikelihoodBuffer placeLikelihoods) { for (PlaceLikelihood placeLikelihood : placeLikelihoods) { Log.i("TAG", String.format("Place %s has likelihood : %g", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood())); } placeLikelihoods.release(); } } ); } catch (Exception e) { Log.i("ERRTBP: ",e.toString()); } } @Override public void onConnected(Bundle bundle) { //super.onStart(); mGoogleAPIClient.connect(); } @Override public void onConnectionSuspended(int i) { mGoogleAPIClient.disconnect(); // super.onStop(); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override protected void attachBaseContext(Context Base) { super.attachBaseContext(Base); MultiDex.install(this); } }

此外,下面提到的错误是我花费大部分时间寻找解决方案的事情。

10-08 14:19:02.840  11979-11979/com.example.GoogleAPIsLearning I/Inside Try﹕ Before GooglePlaceCommunicator
10-08 14:19:02.850  11979-11979/com.example.GoogleAPIsLearning I/dalvikvm﹕ Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
10-08 14:19:02.850  11979-11979/com.example.GoogleAPIsLearning W/dalvikvm﹕ VFY: unable to resolve virtual method 229: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
10-08 14:19:02.850  11979-11979/com.example.GoogleAPIsLearning D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x00c8
10-08 14:19:02.850  11979-11979/com.example.GoogleAPIsLearning I/dalvikvm﹕ DexOpt: access denied from Lcom/google/android/gms/common/GooglePlayServicesUtil; to field Landroid/app/Notification;.extras
10-08 14:19:02.850  11979-11979/com.example.GoogleAPIsLearning W/dalvikvm﹕ VFY: unable to resolve instance field 17
10-08 14:19:02.850  11979-11979/com.example.GoogleAPIsLearning D/dalvikvm﹕ VFY: replacing opcode 0x54 at 0x00e7
10-08 14:19:02.854  11979-11979/com.example.GoogleAPIsLearning I/dalvikvm﹕ Could not find method android.os.UserManager.getApplicationRestrictions, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzah
10-08 14:19:02.854  11979-11979/com.example.GoogleAPIsLearning W/dalvikvm﹕ VFY: unable to resolve virtual method 1384: Landroid/os/UserManager;.getApplicationRestrictions (Ljava/lang/String;)Landroid/os/Bundle;
10-08 14:19:02.854  11979-11979/com.example.GoogleAPIsLearning D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0012
10-08 14:19:02.855  11979-11979/com.example.GoogleAPIsLearning E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzb
10-08 14:19:02.855  11979-11979/com.example.GoogleAPIsLearning W/dalvikvm﹕ VFY: unable to resolve check-cast 25 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/GooglePlayServicesUtil;
10-08 14:19:02.855  11979-11979/com.example.GoogleAPIsLearning D/dalvikvm﹕ VFY: replacing opcode 0x1f at 0x000e
10-08 14:19:02.856  11979-11979/com.example.GoogleAPIsLearning I/dalvikvm﹕ Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzj
10-08 14:19:02.856  11979-11979/com.example.GoogleAPIsLearning W/dalvikvm﹕ VFY: unable to resolve virtual method 483: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
10-08 14:19:02.856  11979-11979/com.example.GoogleAPIsLearning D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000b
10-08 14:19:02.857  11979-11979/com.example.GoogleAPIsLearning I/ERRTBP:﹕ java.lang.NullPointerException
10-08 14:19:02.900  11979-11979/com.example.GoogleAPIsLearning D/libEGL﹕ loaded /vendor/lib/egl/libEGL_mtk.so
10-08 14:19:02.907  11979-11979/com.example.GoogleAPIsLearning D/libEGL﹕ loaded /vendor/lib/egl/libGLESv1_CM_mtk.so
10-08 14:19:02.912  11979-11979/com.example.GoogleAPIsLearning D/libEGL﹕ loaded /vendor/lib/egl/libGLESv2_mtk.so
10-08 14:19:02.937  11979-11979/com.example.GoogleAPIsLearning D/OpenGLRenderer﹕ Enabling debug mode 0

Google Play服务版如下。

enter image description here

1 个答案:

答案 0 :(得分:0)

将此行添加到清单文件

<meta-data android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version"/>