没找到类“android.view.MapFragment”

时间:2014-03-15 20:43:38

标签: android eclipse google-maps

我正在尝试根据以下教程制作Google Maps Android应用: http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

崩溃是因为" android.view.MapFragment"未检测到类:

03-14 03:42:13.219: D/AndroidRuntime(3052): Shutting down VM
03-14 03:42:13.219: W/dalvikvm(3052): threadid=1: thread exiting with uncaught exception (group=0xb3af2ba8)
03-14 03:42:13.249: E/AndroidRuntime(3052): FATAL EXCEPTION: main
03-14 03:42:13.249: E/AndroidRuntime(3052): Process: com.example.test3, PID: 3052
03-14 03:42:13.249: E/AndroidRuntime(3052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test3/com.example.test3.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class MapFragment
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.os.Looper.loop(Looper.java:136)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at java.lang.reflect.Method.invoke(Method.java:515)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at dalvik.system.NativeStart.main(Native Method)
03-14 03:42:13.249: E/AndroidRuntime(3052): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class MapFragment
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.Activity.setContentView(Activity.java:1929)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at com.example.test3.MainActivity.onCreate(MainActivity.java:22)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.Activity.performCreate(Activity.java:5231)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-14 03:42:13.249: E/AndroidRuntime(3052):     ... 11 more
03-14 03:42:13.249: E/AndroidRuntime(3052): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.MapFragment" on path: DexPathList[[zip file "/data/app/com.example.test3-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.test3-1, /system/lib]]
03-14 03:42:13.249: E/AndroidRuntime(3052):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
03-14 03:42:13.249: E/AndroidRuntime(3052):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
03-14 03:42:13.249: E/AndroidRuntime(3052):     ... 20 more
03-14 03:42:23.709: I/Process(3052): Sending signal. PID: 3052 SIG: 9

这是我的AndroidManifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test3"
    android:versionCode="1"
    android:versionName="1.0c" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
    <permission
        android:name="com.example.test3.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.test3.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <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" >
        <activity
            android:name="com.example.test3.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- Google Maps API Key -->
        <meta-data
             android:name="com.google.android.maps.v2.API_KEY"
             android:value="AIzaSyD1XTDQkNMnUtrnVvcocvGeZQsiBV3j3VA" />
    </application>

</manifest>

这是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<MapFragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</MapFragment>

和MainActivity.java:

package com.example.test3;

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

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.example.test3.R;

public class MainActivity extends Activity {
    private GoogleMap googleMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            // Loading map
            initializeMap();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private void initializeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        initializeMap();
    }

}

任何想法会发生什么?

2 个答案:

答案 0 :(得分:0)

我会再看看你的activity_main.xml。您正在指定<MapView ...>标记,该标记将尝试解析为android.view.MapView,从而导致您看到异常。

尝试以下方法;

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

答案 1 :(得分:0)

- 首先,您应该检查APIKEY,并且包名与您的项目相同 - 更改activity_main的xml看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<!--
See this page for more XML attribute options
https://developers.google.com/maps/documentation/android/map#using_xml_attributes
-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      map:mapType="normal"/>

然后更改ActivityMain.java

package com.example.test3;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends Activity {
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        // Loading map
        initializeMap();
    } catch (Exception e) {
        e.printStackTrace();
    }

}
private void initializeMap() {
    if (googleMap == null) {
     GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.map)).getMap();
        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
}
   @Override
    protected void onResume() {
    super.onResume();
    initializeMap();
  }
}

希望它可以帮到你:D