我创建了一个使用谷歌地图的Android应用程序.我正在使用Eclipse,活动布局xml如下.
<?xml version="1.0" enCoding="utf-8"?> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <fragment androID:name="com.Google.androID.gms.maps.SupportMapFragment" androID:ID="@+ID/map" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /> </linearLayout>
这是Activity类
@Suppresslint("ShowToast") public class MapVIEwActivity extends FragmentActivity { static LatLng currentLatLng; LatLng currentLocation; private GoogleMap map; boolean firstPass = true; double currentLat; double currentlong;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_map_vIEw); showCurrentLocationOnMap(); try { showSheltersAndFuelStops(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); }}//This will change when sql connection string is implemented.private voID showSheltersAndFuelStops() throws IOException { List<McDonalds> mcdList = new ArrayList<McDonalds>(); List<Subway> subwayList = new ArrayList<Subway>(); String subwayRoswell1 = "1307 N Main St, Roswell, NM, 88203"; String subwayRoswell2 = "1701 W Second St, Roswell, NM, 88201"; String subwayLC1 = "2001 E Lohman, Las Cruces, NM, 88001"; String subwayLC2 = "2821 N Telshor, Las Cruces, NM 88011"; String mcdonaldsRoswell1 = "720 N Main St, Roswell, NM, 88201"; String mcdonaldsRoswell2 = "1804 S Main St, Roswell, NM, 88203"; String mcdonaldsLC1 = "4810 Mesa Grande, Las Cruces, NM, 88012"; String mcdonaldsLC2 = "571 Walton Blvd, Las Cruces, NM, 88001"; Subway subway = new Subway(" 1307 N Main St, Roswell, NM, 88203", 1.58, 8.0); subwayList.add(subway); Subway subway1 = new Subway(" 1701 W Second St, Roswell, NM, 88201", 2.47, 12.0); subwayList.add(subway1); Subway subsLC = new Subway(" 2001 E Lohman, Las Cruces, NM, 88001", 147.99, 185.0); subwayList.add(subsLC); Subway subsLC1 = new Subway(" 2821 N Telshor Drive, Las Cruces, NM, 88011", 147.12, 200.0); subwayList.add(subsLC1); McDonalds mcdonalds = new McDonalds(" 720 N Main St, Roswell, NM, 88201", 1.08, 8.0); mcdList.add(mcdonalds); McDonalds mcdonalds1 = new McDonalds(" 1804 S Main St, Roswell, NM, 88203", 2.9, 12.0); mcdList.add(mcdonalds1); McDonalds mcdonaldslc = new McDonalds(" 4810 Mesa Grande, Las Cruces, NM, 88012", 143.2, 185.0); mcdList.add(mcdonaldslc); McDonalds mcdonaldslc1 = new McDonalds(" 571 Walton Blvd, Las Cruces, NM, 88001", 147.62, 200.0); mcdList.add(mcdonaldslc1); showFuelStops(subwayRoswell1); showFuelStops(subwayRoswell2); showFuelStops(subwayLC1); showFuelStops(subwayLC2); showShelters(mcdonaldsRoswell1); showShelters(mcdonaldsRoswell2); showShelters(mcdonaldsLC1); showShelters(mcdonaldsLC2);}private voID showFuelStops(String location) throws IOException{ Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses; addresses = geocoder.getFromLocationname(location, 1); if(addresses.size() > 0) { BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromresource(R.drawable.fillingstation); currentLat= addresses.get(0).getLatitude(); currentlong= addresses.get(0).getLongitude(); LatLng subLoc = new LatLng(currentLat, currentlong); Marker fuelMarker = map.addMarker(new MarkerOptions().position(subLoc).icon(subwayBitmapDescriptor).Title("Subway, " + location)); } }private voID showShelters(String location) throws IOException{ Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses; addresses = geocoder.getFromLocationname(location, 1); if(addresses.size() > 0) { BitmapDescriptor subwayBitmapDescriptor = BitmapDescriptorFactory.fromresource(R.drawable.shelter); double latitude= addresses.get(0).getLatitude(); double longitude= addresses.get(0).getLongitude(); LatLng subLoc = new LatLng(latitude, longitude); Marker hamburg = map.addMarker(new MarkerOptions().position(subLoc).icon(subwayBitmapDescriptor).Title("McDonalds, " + location)); } }private voID showCurrentLocationOnMap(){ LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); SupportMapFragment mf= (SupportMapFragment)getSupportFragmentManager().findFragmentByID(R.ID.map); map = mf.getMap(); //map = ((MapFragment) getFragmentManager().findFragmentByID(R.ID.map)).getMap(); LocationListener ll = new MylocationListener(); boolean isGPS = lm.isProvIDerEnabled(LocationManager.GPS_PROVIDER); if (!isGPS){ Intent intent = new Intent("androID.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", true); sendbroadcast(intent); } lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, ll);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.map_vIEw, menu); return true;}/** *MylocationListener class will give the current GPS location *with the help of Location Listener interface */private class MylocationListener implements LocationListener { private boolean zoomed = false; @OverrIDe public voID onLocationChanged(Location location) { if (location != null) { // ---Get current location latitude, longitude--- Log.d("LOCATION CHANGED", location.getLatitude() + ""); Log.d("LOCATION CHANGED", location.getLongitude() + ""); currentLocation = new LatLng(location.getLatitude(), location.getLongitude()); currentLatLng = new LatLng(location.getLatitude(), location.getLongitude()); Marker currentLocationMarker = map.addMarker(new MarkerOptions().position(currentLocation).Title("Current Location")); // Move the camera instantly to hamburg with a zoom of 15. map.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng, 15)); // Zoom in, animating the camera. if (!zoomed) { map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null); zoomed = true; } if (!firstPass){ currentLocationMarker.remove(); } firstPass = false; Toast.makeText(MapVIEwActivity.this,"Latitude = "+ location.getLatitude() + "" +"Longitude = "+ location.getLongitude(), Toast.LENGTH_LONG).show(); } else { //MapVIEwActivity.this.startActivity(new Intent(MapVIEwActivity.this, NoGPSLocActivity.class)); } } @OverrIDe public voID onProvIDerDisabled(String provIDer) { } @OverrIDe public voID onProvIDerEnabled(String provIDer) { } @OverrIDe public voID onStatusChanged(String provIDer, int status, Bundle extras) { }}
}
当我在其他版本的AndroID上运行此应用程序运行良好,但在AndroID 2.3.4的目标上我得到这个致命的例外.
解决方法:
您收到错误是因为您尝试在不支持碎片的AndroID系统上使用Fragment.要为此版本的 *** 作系统启用片段支持,您必须将项目的引用添加到Google-support-v4库.并使用xml中的SupportMapFragment对象而不是MapFragment.你的活动必须扩展FragmentActvitiy,而不是现在可能做的ordinery活动.
您可以在我撰写的博客文章中阅读有关如何向您的应用程序添加Google Map API V2的更多内容以及对erlIEar OS版本的支持:
Google Maps API V2
更新:
请确保以下3个选定的jar文件(红色)位于AndroID DependencIEs文件夹中(绿色):
总结以上是内存溢出为你收集整理的在Android Target 2.3.4上运行Google Maps应用程序会产生致命异常全部内容,希望文章能够帮你解决在Android Target 2.3.4上运行Google Maps应用程序会产生致命异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)