我在我的android应用程序中使用openstrret地图,我使用OSM在我的国家/地区添加了酒店,但是当我运行我的应用程序时,我得到运行时异常,我不知道出了什么问题,而且不能找到导致异常的原因
这是我的代码:
public class MainActivity extends Activity {
MyItemizedOverlay myItemizedOverlay = null;
MyLocationOverlay myLocationOverlay = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
////zoom////
MapController myMapController = mapView.getController();
myMapController.setZoom(10);
ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(this);
mapView.getOverlays().add(myScaleBarOverlay);
////zoom////
Drawable marker=getResources().getDrawable(R.drawable.marker) ;
// Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
ResourceProxy resourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
myItemizedOverlay = new MyItemizedOverlay(marker, resourceProxy);
mapView.getOverlays().add(myItemizedOverlay);
ExtendedOverlayItem ex=new ExtendedOverlayItem("Funduq Shibbird", "Funduq Shibbird", new GeoPoint(31.4657,35.1347), null);
ex.setDescription("Funduq Shibbird");
Drawable icon = getResources().getDrawable(R.drawable.ic_launcher);
ex.setImage(icon);
ex.setMarker(marker);
final ArrayList<ExtendedOverlayItem> roadItems = new ArrayList<ExtendedOverlayItem>();
roadItems.add(ex);
ItemizedOverlayWithBubble<ExtendedOverlayItem> roadNodes = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this, roadItems, mapView);
mapView.getOverlays().add(roadNodes);
GeoPoint hotel2 = new GeoPoint(31.5522,35.1310);
myItemizedOverlay.addItem(hotel2, "Funduq Siti Inn Wahid ", "Funduq Siti Inn Wahid ");
GeoPoint hotel3 = new GeoPoint(31.5353,35.1238);
myItemizedOverlay.addItem(hotel3, "Funduq Siti Inn Ithnayn", "Funduq Siti Inn Ithnayn");
GeoPoint hotel4 = new GeoPoint(31.4225,35.1155);
myItemizedOverlay.addItem(hotel4, "Funduq Shibbird", "Funduq Shibbird");
GeoPoint hotel5 = new GeoPoint(31.4225,35.1155);
myItemizedOverlay.addItem(hotel5, "Funduq al Ayn", "Funduq al Ayn");
GeoPoint hotel6 = new GeoPoint( 31.3131,34.2623);
myItemizedOverlay.addItem(hotel6, "Funduq al Amal", "Funduq al Amal");
GeoPoint hotel7 = new GeoPoint( 31.3123,342559);
myItemizedOverlay.addItem(hotel7, "Funduq Adam", "Funduq Adam");
GeoPoint hotel8 = new GeoPoint(31.3127,34.262);
myItemizedOverlay.addItem(hotel8, "Funfuq Filastin", "Funfuq Filastin");
GeoPoint hotel9 = new GeoPoint(31.3136,34.2610);
myItemizedOverlay.addItem(hotel9, "Cliff Hotel", "Cliff Hotel");
GeoPoint hotel10 = new GeoPoint(31.3139,34.2614);
myItemizedOverlay.addItem(hotel10, "Al Quds Hotel", "Al Quds Hotel");
GeoPoint hotel11 = new GeoPoint(31.3132,34.267);
myItemizedOverlay.addItem(hotel11, "Beach Hotel", "Beach Hotel");
GeoPoint hotel12 = new GeoPoint(31.4218,35.1217);
myItemizedOverlay.addItem(hotel12, "Al Salam Hotel", "Al Salam Hotel");
GeoPoint hotel13 = new GeoPoint(31.4243,35.1212);
myItemizedOverlay.addItem(hotel13, "Bethlehem Hotel", "Bethlehem Hotel");
GeoPoint hotel14 = new GeoPoint(32.1315,35.1426);
myItemizedOverlay.addItem(hotel14, "Al Qasr Hotel", "Al Qasr Hotel");
GeoPoint hotel15 = new GeoPoint(31.5344,35.1217);
myItemizedOverlay.addItem(hotel15, "Movinpick Hotel", "Movinpick Hotel");
///////////////////////////////////
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
// mapView.getController().
}
});
}
public void List_hotels_ps(View v){
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableFollowLocation();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableFollowLocation();
}
// myLocationOverlay.enableMyLocation();
// }
}
这是logCat:
11-10 16:58:51.362: E/AndroidRuntime(2694): FATAL EXCEPTION: main
11-10 16:58:51.362: E/AndroidRuntime(2694): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.openstreetmap/com.example.openstreetmap.MainActivity}: android.view.InflateException: Binary XML file line #14: Error inflating class org.osmdroid.views.MapView
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.os.Looper.loop(Looper.java:137)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-10 16:58:51.362: E/AndroidRuntime(2694): at java.lang.reflect.Method.invokeNative(Native Method)
11-10 16:58:51.362: E/AndroidRuntime(2694): at java.lang.reflect.Method.invoke(Method.java:525)
11-10 16:58:51.362: E/AndroidRuntime(2694): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-10 16:58:51.362: E/AndroidRuntime(2694): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-10 16:58:51.362: E/AndroidRuntime(2694): at dalvik.system.NativeStart.main(Native Method)
11-10 16:58:51.362: E/AndroidRuntime(2694): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class org.osmdroid.views.MapView
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-10 16:58:51.362: E/AndroidRuntime(2694): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.Activity.setContentView(Activity.java:1895)
11-10 16:58:51.362: E/AndroidRuntime(2694): at com.example.openstreetmap.MainActivity.onCreate(MainActivity.java:36)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.Activity.performCreate(Activity.java:5133)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-10 16:58:51.362: E/AndroidRuntime(2694): ... 11 more
11-10 16:58:51.362: E/AndroidRuntime(2694): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.osmdroid.views.MapView" on path: DexPathList[[zip file "/data/app/com.example.openstreetmap-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.openstreetmap-2, /vendor/lib, /system/lib]]
11-10 16:58:51.362: E/AndroidRuntime(2694): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
11-10 16:58:51.362: E/AndroidRuntime(2694): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-10 16:58:51.362: E/AndroidRuntime(2694): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
11-10 16:58:51.362: E/AndroidRuntime(2694): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
答案 0 :(得分:0)
原因是你在编译时有osmdroid.jar但在运行时没有。这个jar没有加载到bin目录中,这是apk的lib目录。
将此jar加载到您的apk 右键单击项目 - &gt; properties-&gt; java buildpath-&gt; order and export&gt;检查osmdroid jar
或
将osmdroid jar复制并粘贴到libs文件夹中,然后将其从buildpath中删除(othervise将两个相同的jar添加到bin目录中,这将导致另一个问题)