Android Maps App - 由装载机dalvik.system中的java.lang.classnotfoundexception android.view.fragment引起

时间:2013-07-28 18:10:07

标签: android

我正在尝试执行这个简单的教程(http://mobile.tutsplus.com/tutorials/android/android-sdk-working-with-google-maps-application-setup/),但标题中提到了一个问题。当我尝试在真实设备(Android 2.3.6)上运行此应用程序时,我在LogCat中收到这些错误:

07-28 20:59:08.835: I/ApplicationPackageManager(3807): cscCountry is not German : SEB
07-28 20:59:08.851: D/AndroidRuntime(3807): Shutting down VM
07-28 20:59:08.851: W/dalvikvm(3807): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-28 20:59:08.867: E/AndroidRuntime(3807): FATAL EXCEPTION: main
07-28 20:59:08.867: E/AndroidRuntime(3807): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.interestmapapp/com.example.interestmapapp.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.os.Looper.loop(Looper.java:130)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.ActivityThread.main(ActivityThread.java:3687)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at java.lang.reflect.Method.invoke(Method.java:507)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at dalvik.system.NativeStart.main(Native Method)
07-28 20:59:08.867: E/AndroidRuntime(3807): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class fragment
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:209)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.Activity.setContentView(Activity.java:1657)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at com.example.interestmapapp.MainActivity.onCreate(MainActivity.java:13)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-28 20:59:08.867: E/AndroidRuntime(3807):     ... 11 more
07-28 20:59:08.867: E/AndroidRuntime(3807): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.interestmapapp-1.apk]
07-28 20:59:08.867: E/AndroidRuntime(3807):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.view.LayoutInflater.createView(LayoutInflater.java:471)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
07-28 20:59:08.867: E/AndroidRuntime(3807):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
07-28 20:59:08.867: E/AndroidRuntime(3807):     ... 19 more

我的MainActivity.java:

package com.example.interestmapapp;

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

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我的AndroidManifest.xml:

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

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

    <permission
        android:name="com.example.interestmapapp.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.interestmapapp.permission.MAPS_RECEIVE" />
    <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.maps.v2.API_KEY"
            android:value="MY_API_KEY" />
>
        <activity
            android:name="com.example.interestmapapp.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>

</manifest>

我的activity_main.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/the_map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    map:cameraTilt="45"
    map:cameraZoom="14" />

我已成功将我的API密钥添加到Manifest,我也将Google Play服务导入到项目中(如教程所述)。谁知道我该怎么办?谢谢你的帮助。

1 个答案:

答案 0 :(得分:9)

你的min sdk是8.你应该使用SupportMapFragment

<fragment
class="com.google.android.gms.maps.SupportMapFragment"
...

您的课程必须延长FragmentActivity

还要确保导入以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;