防止谷歌地图对象上的NPE

时间:2014-07-19 04:52:55

标签: java android eclipse google-maps

我正在使用谷歌地图创建一个项目。但是谷歌地图对象在初学者中没有初始化。它发生在课程的后期阶段。但是当没有互联网连接时,该课程的其他部分将不会加载那是我在地图对象的监听器上得到一个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);

            }
        });


    }

1 个答案:

答案 0 :(得分:0)

1)使用findViewById()查找mapView。或者你的MapFragment,如果你是这样做的。

2)在mapview或mapfragment上调用getMap()。