Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 防止google maps对象上的NPE_Java_Android_Eclipse_Google Maps - Fatal编程技术网

Java 防止google maps对象上的NPE

Java 防止google maps对象上的NPE,java,android,eclipse,google-maps,Java,Android,Eclipse,Google Maps,我正在使用google maps创建一个项目。但是google map对象在开始时没有初始化。它发生在课程的后期。但是当没有internet连接时,类的另一部分不会加载。那就是当我在map对象的侦听器中获得NPE时。如何防止google map对象上的NPE。我的oncreate方法 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); se

我正在使用google maps创建一个项目。但是google map对象在开始时没有初始化。它发生在课程的后期。但是当没有internet连接时,类的另一部分不会加载。那就是当我在map对象的侦听器中获得NPE时。如何防止google map对象上的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) 使用findViewById()查找地图视图。或者你的地图碎片,如果你是这样做的


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

为什么不将侦听器包装在一个简单的空检查中?例如,
if(googlemap!=null){/*您的侦听器*/}
这是在类中进一步使用的代码:googlemap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();。我可以在oncreate中使用它吗?在创建片段后,您可以随时使用它。我不知道这在你的代码里。如果它不是在onCreate中创建的,那么您还不能设置click侦听器。