我的代码中有这个功能
private boolean isGoogleServiceAvailable() {
try {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); //ERROR
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
}
catch (Exception ex) {
Toast.makeText(this, R.string.googleMapsServiceNotAvailable, Toast.LENGTH_LONG).show();
}
return false;
}
但是我在这行代码中得到java.lang.NullPointerException
例外:
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
可能是因为我没有正确传递上下文。
我的班级声明如下:
public class MapEngine extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, OnItemSelectedListener {
我从我的主要活动的onCreate
调用方法:
public class AppMain extends ListActivity{
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
// InitiateMap
mapObj.isGoogleServiceAvailable();
}
您能告诉我如何将正确的上下文传递给isGooglePlayServicesAvailable
函数吗?
答案 0 :(得分:0)
我认为这是因为您使用的是未创建的活动方法。在启动地图之前,请尝试从主应用程序检查播放服务。
public class AppMain extends ListActivity{
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
try {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); //ERROR
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
}
else if (GooglePlayServicesUtil.isUserRecoverableError(isAvailable)) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, AppMain.this, GPS_ERRORDIALOG_REQUEST);
dialog.show();
}
}
catch (Exception ex) {
Toast.makeText(this, R.string.googleMapsServiceNotAvailable, Toast.LENGTH_LONG).show();
}
}