2013-04-04 49 views
3

我得到了一個kml文件存儲在我的sdcard.I想通過我的應用程序中的谷歌地圖打開它,所以我用下面的代碼,但我得到錯誤說「android illegalstateexception無法執行活動的方法「android - 如何在android中打開kml文件

我不想繪製地圖,所有隻是我想打開一個已存儲在我的SD卡並顯示它的kml文件。

當我谷歌我有很多項目相關的基於數據繪製地圖,這我不想要。

我的代碼:

public void showmap(View v){ 

    File file = new File(Environment.getExternalStorageDirectory(), "10.kml"); 
    Intent map_intent = new Intent(Intent.ACTION_VIEW); 
    map_intent.setClassName("com.google.earth", "com.google.earth.EarthActivity"); 
    map_intent.setDataAndType(Uri.fromFile(file), "application/vnd.google-earth.kml+xml"); 
    startActivity(map_intent); 
} 

我的Android清單文件

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

    <uses-sdk 
     android:minSdkVersion="15" 
     android:targetSdkVersion="16" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.VIBRATE"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="android.permission.CAMERA" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.lyold.LoginActivity" 
      android:screenOrientation="landscape" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:screenOrientation="landscape" 
      android:name=".Home" /> 

     <activity android:name="com.google.earth.EarthActivity" android:screenOrientation="landscape"/> 




    </application> 

</manifest> 

我logcat的錯誤:

04-04 18:01:05.387: E/AndroidRuntime(5846): FATAL EXCEPTION: main 
04-04 18:01:05.387: E/AndroidRuntime(5846): java.lang.IllegalStateException: Could not execute method of the activity 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.view.View$1.onClick(View.java:3044) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.view.View.performClick(View.java:3511) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.view.View$PerformClick.run(View.java:14105) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.os.Handler.handleCallback(Handler.java:605) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.os.Looper.loop(Looper.java:137) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at dalvik.system.NativeStart.main(Native Method) 
04-04 18:01:05.387: E/AndroidRuntime(5846): Caused by: java.lang.reflect.InvocationTargetException 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.view.View$1.onClick(View.java:3039) 
04-04 18:01:05.387: E/AndroidRuntime(5846): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/10.kml typ=application/vnd.google-earth.kml+xml } 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.app.Activity.startActivityForResult(Activity.java:3190) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at android.app.Activity.startActivity(Activity.java:3297) 
04-04 18:01:05.387: E/AndroidRuntime(5846):  at com.example.ashadegreen.Basement.showmap(Basement.java:106) 

我是新來android.Please幫助me..Your幫助不勝感激。

回答

0

檢查AndroidManifest.xml FILE相關權限,並確保您嘗試打開的文件路徑(10.kml)。除此之外,您的代碼中一切都很好。

+0

文件路徑是正確的 – sdg 2013-04-04 13:22:08