threadid = 1:线程退出,未捕获异常(group = 0x40dc31f8)

时间:2013-01-29 11:46:55

标签: android google-maps-android-api-2

您好我在尝试运行地图时遇到此错误。 这是我的代码

mapview = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();



    try {
        /**
        * Create a new instance of the SAX parser
        **/
        SAXParserFactory saxPF = SAXParserFactory.newInstance();
        SAXParser saxP = saxPF.newSAXParser();
        XMLReader xmlR = saxP.getXMLReader();
       // URL url = new URL("http://KML.php"); // URL of the XML
        /**
        * Create the Handler to handle each of the XML tags.
        **/
        InputStream is = getAssets().open("NewFile.xml");
        XMlHandler myXMLHandler = new XMlHandler();
        xmlR.setContentHandler(myXMLHandler);
      //  xmlR.parse(new InputSource(url.openStream()));
        xmlR.parse(new InputSource(is));
        sitesList = XMlHandler.getXMLData();

        //data= myXMLHandler.getXMLData();
    } catch (Exception e) {
        System.out.println(e);
    }
    //System.out.print(sitesList.getDescription().toString());
    for (int i = 0; i < sitesList.getCompany().size(); i++) {
        System.out.print(sitesList.getDescription().get(i).toString());
        System.out.print(sitesList.getLatitude().get(i).toString());
        System.out.print(sitesList.getLongitude().get(i).toString());
        //System.out.print(sitesList.getAddress().get(i).toString());
         compan22 = sitesList.getDescription().get(i).toString();
         compan11 = sitesList.getCompany().get(i).toString();
        double markerSnippet2 = (Double.parseDouble(sitesList.getLatitude().get(i).toString()));
        double markerSnippet3 = (Double.parseDouble(sitesList.getLongitude().get(i).toString()));
         ITALYwww=new LatLng(markerSnippet2,markerSnippet3);


            Marker testXml = mapview.addMarker(new MarkerOptions().title(compan22).position(ITALYwww).snippet(compan11));


    }

   LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = service.getBestProvider(criteria, false);
    Location location = service.getLastKnownLocation(provider);
    LatLng userLocation = new LatLng(location.getLatitude(),location.getLongitude());

    String longitude = "Longitude: " +location.getLongitude();    
    Log.v(TAG, longitude);  
    String latitude = "Latitude: " +location.getLatitude();  
    Log.v(TAG, latitude);  


        String cityName=null;   
        String addressName=null;
          Geocoder gcd = new Geocoder(getBaseContext(),   
       Locale.getDefault());               
          List<Address>  addresses;    
          try {    
          addresses = gcd.getFromLocation(location.getLatitude(), location  
       .getLongitude(), 1);    
          if (addresses.size() > 0)    
             System.out.println(addresses.get(0).getLocality());    
             cityName=addresses.get(0).getLocality();   
             addressName=addresses.get(0).getAddressLine(0);

            } catch (IOException e) {              
            e.printStackTrace();    
          }         
          String s = longitude+"\n"+latitude +  
       "\n\nMy Currrent City is: "+cityName;  
    Marker currentLoc = mapview.addMarker(new MarkerOptions().position(userLocation).title(cityName).snippet(addressName).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
        mapview.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 55));
        mapview.animateCamera(CameraUpdateFactory.zoomTo(10), 5000, null);

和LogCat

01-29 12:35:34.130: I/long(5019): -1.054687
01-29 12:35:34.150: W/dalvikvm(5019): threadid=1: thread exiting with uncaught exception (group=0x40dc31f8)
01-29 12:35:34.150: E/AndroidRuntime(5019): FATAL EXCEPTION: main
01-29 12:35:34.150: E/AndroidRuntime(5019): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemapsv1/com.example.googlemapsv1.MainActivity}: java.lang.NullPointerException
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1960)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.os.Looper.loop(Looper.java:137)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.ActivityThread.main(ActivityThread.java:4476)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at dalvik.system.NativeStart.main(Native Method)
01-29 12:35:34.150: E/AndroidRuntime(5019): Caused by: java.lang.NullPointerException
01-29 12:35:34.150: E/AndroidRuntime(5019):     at com.example.googlemapsv1.MainActivity.onCreate(MainActivity.java:121)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.Activity.performCreate(Activity.java:4636)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
01-29 12:35:34.150: E/AndroidRuntime(5019):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
01-29 12:35:34.150: E/AndroidRuntime(5019):     ... 11 more

1 个答案:

答案 0 :(得分:1)

你的问题可能来自这条线 -

Location location = service.getLastKnownLocation(provider);

来自getLastKnownLocation上的文档,

  

如果当前禁用了提供程序,则返回null。

由于null是可能的返回值,因此您应该在使用之前检查location是否为null来防范这种可能性。