0

我在我的 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)
4

1 回答 1

0

原因是您在编译时有 osmdroid.jar,但在运行时没有。此 jar 未加载到作为 apk 的 lib 目录的 bin 目录中。

将此jar加载到您的apk右键单击项目->属性->java buildpath->order and export>检查osmdroid jar

或者

将 osmdroid jar 复制并粘贴到 libs 文件夹中,然后将其从 buildpath 中删除(否则两个相同的 jar 将添加到 bin 目录中,这将导致另一个问题)

于 2013-11-13T20:58:36.437 回答