【问题标题】:Didn't find class "android.view.MapFragment"没有找到类“android.view.MapFragment”
【发布时间】:2014-03-15 20:43:38
【问题描述】:

我正在尝试根据以下教程制作 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();
    }

}

有什么想法吗??

【问题讨论】:

    标签: android eclipse google-maps


    【解决方案1】:

    我会再看看你的activity_main.xml。您正在指定 &lt;MapView ...&gt; 标记,该标记将尝试解析为 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>
    

    【讨论】:

    • 当我改变了这一点,并添加了行“”到 AndroidManifest.xml,它起作用了。谢谢!
    【解决方案2】:

    -首先,您应该检查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

    【讨论】:

    • 我试过这个,但它给了我错误 - 我认为它无法找到 SupportMapFragment 类。然而,marcus.ramsden 发布的解决方案确实有效,所以我不会继续尝试这种方法。谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多