2012-05-16 141 views
1

我想使用osmdroid製作一個簡單的android應用程序,它應該只顯示一張地圖。 我在項目中也包含了osmdroid-android-3.0.8.jar和slf4j-android-1.5.8.jar。Osmdroid示例不起作用

代碼:

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 

任何人都可以指出我要去的地方錯了嗎?

+0

檢查org.osmdroid.views.MapView的咒語。可能它是錯誤的或路徑不正確 – Blackbelt

+1

可能是因爲你需要創建一個'libs'文件夾並把你的罐子放在那裏。 (最新的SDK工具需要此)請參閱http://stackoverflow.com/questions/10046298/android-update-17-seems-incompatible-with-external-jars/10046725#10046725 – NickT

回答

-1

您需要在您的清單文件中添加額外的權限是這樣的:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
6

放SLF4J-的android.jar和osmdroid-的android.jar到項目就可以解決問題的文件夾「庫」。我已經測試過

+0

感謝它爲我工作! – programmer