Android 创建映射示例时出现空指针异常

Android 创建映射示例时出现空指针异常,android,google-maps,exception,Android,Google Maps,Exception,它不断地为我得到这个错误,并且在模拟器上显示force close。没有走错方向。实际上,我正在创建一个程序,参考一本书来获取位置数据。以下是我的程序和logcat错误。我引用了这些,但我无法从中找到解决方案。有人能告诉我有什么问题吗?甚至我已经在清单中添加了这个 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.pe

它不断地为我得到这个错误,并且在模拟器上显示force close。没有走错方向。实际上,我正在创建一个程序,参考一本书来获取位置数据。以下是我的程序和logcat错误。我引用了这些,但我无法从中找到解决方案。有人能告诉我有什么问题吗?甚至我已经在清单中添加了这个

<uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
和在应用程序标记中

<uses-library android:name="com.google.android.maps"/>
这是我的.java文件

public class LBSMap extends MapActivity {

    MapView mapView;
    MapController mc;
    GeoPoint p;

    LocationManager lm;
    LocationListener locationListener;

    private class MapOverlay extends com.google.android.maps.Overlay
    {
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
        {
            super.draw(canvas, mapView, shadow);
            Point screenPts=new Point();
            mapView.getProjection().toPixels(p, screenPts);

            Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.pushpin);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50,null);
            return true;
        }

        public boolean onTouchEvent(MotionEvent event,MapView mapView)
        {
            if(event.getAction()==1)
            {
                GeoPoint p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());

            Geocoder geoCoder=new Geocoder(getBaseContext(),Locale.getDefault());
            try{
                List<Address> addresses=geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1);
                String add="";
                if(addresses.size()>0)
                {
                    for(int i=0;i<addresses.get(0).getMaxAddressLineIndex();i++)
                        add +=addresses.get(0).getAddressLine(i) + "\n";
                }
                Toast.makeText(getBaseContext(),add,Toast.LENGTH_SHORT).show();

            }catch(IOException e){
                e.printStackTrace();
            }
            return true;
            }
            return false;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lbsmap);

        mapView=(MapView)findViewById(R.id.mapView);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);
        mapView.setTraffic(true);

        mc=mapView.getController();

        Geocoder geoCoder=new Geocoder(this,Locale.getDefault());

        try{
            List<Address> addresses=geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1); //line 103
            String add="";
            if(addresses.size()>0)
            {
                for(int i=0;i<addresses.get(0).getMaxAddressLineIndex();i++)
                    add +=addresses.get(0).getAddressLine(i) + "\n";
            }
            Toast.makeText(getBaseContext(),add,Toast.LENGTH_SHORT).show();

        }catch(IOException e){
            e.printStackTrace();
        }

        MapOverlay mapOverlay=new MapOverlay();
        List<Overlay> listOfOverlays=mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);

        mapView.invalidate();

        lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationListener = new MyLocationListener();
    }

    public void onResume()
    {
        super.onResume();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

    public void onPause()
    {
        super.onPause();
        lm.removeUpdates(locationListener);
    }

    private class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location loc)
        {
            if(loc!=null){
                Toast.makeText(getBaseContext(), "Location changed : Lat: "+loc.getLatitude()+ "Lng: "+loc.getLongitude(), Toast.LENGTH_SHORT).show();
                p=new GeoPoint((int)(loc.getLatitude()*1E6),(int)(loc.getLongitude()*1E6));
                mc.animateTo(p);
                mc.setZoom(18);
            }

        }
        public void onProviderDisabled(String provider)
        {}
        public void onProviderEnabled(String provider)
        {}
        public void onStatusChanged(String provider, int status, Bundle extras)
        {}
    }


    @Override
    public boolean isRouteDisplayed()
    {
        return false;
    }
}
下面是logcat的屏幕截图

我猜在onCreate中,地质点p是空的,因为在覆盖代码中为其赋值时,使用

GeoPoint p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
这将创建另一个局部变量,而不是使用全局变量。尝试使用:

p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());

您可能需要在声明中添加最后一个修饰符,使其在另一个类中工作。

请检查您是否具有SDK版本>9的internet访问代码

// Allow network access

    if (android.os.Build.VERSION.SDK_INT > 9) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

    }

@RaghavSood和umar我用第103行编辑了我的问题。请检查。谢谢您的回复。试过这个。仍然显示相同的错误:当我在p=mapView.getProjection.FromPixelsInEvent.getX、intevent.getY行之前添加sop时;它将p对象的值设为null。但是为什么呢?我猜既然你在onTouchEvent中指定了p,那么在你触摸地图之前它将是空的。是的。我也有同样的想法。但书中是这样写的。当我在onCreate方法中将p初始化为某些值时,它正在工作。顺便说一句,为什么现在只有一个android聊天室,而我无法进入那个聊天室。很高兴它能工作。还有一些安卓房间。你可能无法接触到旁边有钻石的人,因此可能无法交谈。