Android地图应用程序正在崩溃

时间:2013-12-17 22:23:54

标签: android api google-maps

我正在尝试创建一个简单的Map View应用程序。我浏览了android developers documentation并复制了粘贴的 MyActivity.java 类和 activity_main.xml 布局。当我开始使用我的真实设备(Android v2.3.6)时,该应用程序正在崩溃。包浏览器,清单文件和logcat错误如下所示。

enter image description here

MainActivity.java:

package com.timur.mapapplication;

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>

的AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.timur.mapapplication.MainActivity"
            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>

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyDQ4pnYq3on6WFJsE..." />

</manifest>

LOGCAT错误:

12-18 00:03:05.626: E/AndroidRuntime(18591): FATAL EXCEPTION: main
12-18 00:03:05.626: E/AndroidRuntime(18591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.timur.mapapplication/com.timur.mapapplication.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.os.Looper.loop(Looper.java:130)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.ActivityThread.main(ActivityThread.java:3693)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at java.lang.reflect.Method.invokeNative(Native Method)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at java.lang.reflect.Method.invoke(Method.java:507)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at dalvik.system.NativeStart.main(Native Method)
12-18 00:03:05.626: E/AndroidRuntime(18591): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:215)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.Activity.setContentView(Activity.java:1663)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at com.timur.mapapplication.MainActivity.onCreate(MainActivity.java:11)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-18 00:03:05.626: E/AndroidRuntime(18591):    ... 11 more
12-18 00:03:05.626: E/AndroidRuntime(18591): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.timur.mapapplication-1.apk]
12-18 00:03:05.626: E/AndroidRuntime(18591):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-18 00:03:05.626: E/AndroidRuntime(18591):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
12-18 00:03:05.626: E/AndroidRuntime(18591):    ... 19 more

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

MapFragment设置为8时,您正在使用minSdkVersion。您应该使用SupportMapFragment,而不是这样:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

另外,只是一个建议,不要发布你的API密钥,它应该保持私密,无论是谁在开发应用程序。

答案 1 :(得分:1)

正如Emmanuel建议的那样,将MapFragment更改为SupportMapFragment。

添加它,在java类中扩展FragmentActivity而不是Activity。看看它现在是否有效。