我在我的 SD 卡中存储了一个 kml 文件。我想通过我的应用程序中的谷歌地图打开它,所以我使用了下面的代码,但我收到错误消息“android 非法状态异常无法执行活动的方法”
我不想绘制地图,我只想打开一个已经存储在我的 sdcard 中的 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的新手。请帮助我..非常感谢您的帮助。