I am using below code to get advertising id in android, but its giving following exception.
06-12 12:14:19.034: E/AndroidRuntime(13631): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.authorwjf.amianemulator/com.authorwjf.amianemulator.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.android.gms.ads.identifier.AdvertisingIdClient$Info.getId()' on a null object reference
I have added google play service library and meta tag in android manifest file.
I am using below code in activity's oncreate method.
Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this);
} catch (IOException e) {
} catch (GooglePlayServicesNotAvailableException e) {
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String AdId = adInfo.getId();
System.out.println("AdId :: "+AdId);
Some doubts ::
does genymotion support advertising id?
is advertising id available in all the android phones which is using 4.0 and above?
Please help me to get the advertising id.
You receive this exception because the Google Play Services are not installed on the device (or emulator).
As you can read in the Google Play Services documentation:
You can check if the Google Play services are installed with isGooglePlayServicesAvailable method, for example:
Yes, but you need to install the Google Play Service manually. See this answer.
No, Google Play Services could be not available.