I am trying to create small app which simply connects to Google Place API and expected output is to get current location in Json data.
But before that, I am facing this error. Please help. I have tried lots of things and none of them seems to be working. If anyone knows the best way to learn this (with current Google Place APIs), Please suggest me as I am tired of doing configuration for two weeks and not writing single line of code instead.
Here is the code for the same. (I am using the Getting Started code from Google Developers guides.) It should return the likely places but it only logs the error.
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");
}
}
AndroidManifext.xml (I have removed API key from below file just to post it here.)
<?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>
GooglePlaceCommunicator.java
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);
}
}
As you can see in above class, I have changed AttachBaseContext method as well because I am running this application directly on my android device (4.1.2 - API16)
Log contains something as below. and that is the error I am not able to solve despite wasting last 2 weeks trying different approaches.
As you can see it goes inside Communicator class and logs data but at the end it just logs null pointer exception.
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
Also, below mentioned error is something where I have spent most of the time looking for the solution.
Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzb
Google Play Services version is as below.