android - 如何在android中打开kml文件

时间:2013-04-04 12:44:02

标签: android google-maps kml

我有一个kml文件存储在我的SD卡中。我想通过我的应用程序中的谷歌地图打开它,所以我使用下面的代码,但我收到错误说“android illegalstateexception无法执行活动的方法”< / p>

我不想绘制地图,只是我想打开一个已经存储在我的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的新手。请帮助我。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

检查AndroidManifest.xml FILE相关权限,并确保您尝试打开的文件路径(10.kml)。除此之外,你的代码中的一切都很好。