【发布时间】:2014-07-19 04:52:55
【问题描述】:
我正在使用谷歌地图创建一个项目。但是谷歌地图对象在开始时没有初始化。它发生在课程的后期。但是当没有互联网连接时,课程的另一部分不会加载。那是当我在地图对象的侦听器处获得 NPE 时。如何防止谷歌地图对象上的 NPE。我的 oncreate 方法。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_categories=(ListView) findViewById(R.id.list_categories);
custom_layout = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout,null);
iv_category_logo=(ImageView) custom_layout.findViewById(R.id.iv_category_logo);
cd = new ConnectionDetector(MainActivity.this);
Config.isInternetPresent = cd.isConnectingToInternet();
if (!Config.isInternetPresent) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// Shuld be fail icon
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage("Connection Not Available !" + "\n"
+ "Please enable your Internet Connection");
builder.setTitle("INTERNET CONNECTION");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
else{
getcurrentlocation();
}
//I am getting the NPE at the googleMap object below.How do i initialise it?
googleMap.setOnMapClickListener(new OnMapClickListener() {
public void onMapClick(LatLng arg0) {
Intent i=new Intent(getApplicationContext(),MapFullScreen.class);
startActivity(i);
}
});
}
【问题讨论】:
-
为什么不将侦听器包装在一个简单的空检查中?即
if(googlemap!=null){ /*your listener*/ }
标签: java android eclipse google-maps