1

我想使用 osmdroid 制作一个简单的 android 应用程序,它应该只显示一张地图。我也在项目中包含了 osmdroid-android-3.0.8.jar 和 slf4j-android-1.5.8.jar 。

代码:

package osmdemo.demo;

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;

import android.app.Activity;
import android.os.Bundle;

public class Osm extends Activity {

private MapView         mMapView;
private MapController   mMapController;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
       mMapView = (MapView) findViewById(R.id.mapview);
       mMapView.setTileSource(TileSourceFactory.MAPNIK);
       mMapView.setBuiltInZoomControls(true);
       mMapController = mMapView.getController();
       mMapController.setZoom(13);
       GeoPoint gPt = new GeoPoint(51500000, -150000);
       //Centre map near to Hyde Park Corner, London
       mMapController.setCenter(gPt);

   }
}

main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <org.osmdroid.views.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:id="@+id/mapview"
        ></org.osmdroid.views.MapView>

</LinearLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="osmdemo.demo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-feature android:name="android.hardware.location.network" />
<uses-feature android:name="android.hardware.location.gps" />   
<uses-feature android:name="android.hardware.wifi" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Osm"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

错误:

05-16 17:29:32.612: W/dalvikvm(361): threadid=1: thread exiting with uncaught exception      (group=0x40015560)
05-16 17:29:32.702: E/AndroidRuntime(361): FATAL EXCEPTION: main
05-16 17:29:32.702: E/AndroidRuntime(361): java.lang.RuntimeException: Unable to start    activity ComponentInfo{osmdemo.demo/osmdemo.demo.Osm}: android.view.InflateException: Binary XML file line #7: Error inflating class org.osmdroid.views.MapView
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.os.Looper.loop(Looper.java:123)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-16 17:29:32.702: E/AndroidRuntime(361):  at java.lang.reflect.Method.invokeNative(Native Method)
05-16 17:29:32.702: E/AndroidRuntime(361):  at java.lang.reflect.Method.invoke(Method.java:507)
05-16 17:29:32.702: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-16 17:29:32.702: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-16 17:29:32.702: E/AndroidRuntime(361):  at dalvik.system.NativeStart.main(Native Method)
05-16 17:29:32.702: E/AndroidRuntime(361): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class org.osmdroid.views.MapView
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-16 17:29:32.702: E/AndroidRuntime(361):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.Activity.setContentView(Activity.java:1657)
05-16 17:29:32.702: E/AndroidRuntime(361):  at osmdemo.demo.Osm.onCreate(Osm.java:19)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-16 17:29:32.702: E/AndroidRuntime(361):  ... 11 more
05-16 17:29:32.702: E/AndroidRuntime(361): Caused by: java.lang.ClassNotFoundException: org.osmdroid.views.MapView in loader dalvik.system.PathClassLoader[/data/app/osmdemo.demo-1.apk]
05-16 17:29:32.702: E/AndroidRuntime(361):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-16 17:29:32.702: E/AndroidRuntime(361):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-16 17:29:32.702: E/AndroidRuntime(361):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.view.LayoutInflater.createView(LayoutInflater.java:471)
05-16 17:29:32.702: E/AndroidRuntime(361):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
05-16 17:29:32.702: E/AndroidRuntime(361):  ... 20 more

谁能指出我哪里出错了?

4

2 回答 2

6

将 slf4j-android.jar 和 osmdroid-android.jar 放到项目中的文件夹 'libs' 可以解决问题。我测试过

于 2012-12-29T05:23:16.857 回答
-1

您需要在清单文件中添加这样的额外权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-06-07T07:39:48.273 回答