简体   繁体   English

在Android中使用Google Map V2时发生ClassNotFoundException

[英]ClassNotFoundException while using Google Map V2 in Android

Log Cat 原木猫

03-11 09:43:55.428: E/dalvikvm(1518): Could not find class 'com.google.android.gms.maps.MapFragment', referenced from method com.example.mapdemo2.MapDemo2.onCreate
03-11 09:43:55.428: W/dalvikvm(1518): VFY: unable to resolve check-cast 696 (Lcom/google/android/gms/maps/MapFragment;) in Lcom/example/mapdemo2/MapDemo2;
03-11 09:43:55.498: D/dalvikvm(1518): VFY: replacing opcode 0x1f at 0x0013
03-11 09:43:55.908: D/AndroidRuntime(1518): Shutting down VM
03-11 09:43:55.908: W/dalvikvm(1518): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-11 09:43:55.958: E/AndroidRuntime(1518): FATAL EXCEPTION: main
03-11 09:43:55.958: E/AndroidRuntime(1518): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo2/com.example.mapdemo2.MapDemo2}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
03-11 09:43:55.958: E/AndroidRuntime(1518):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at android.os.Looper.loop(Looper.java:137)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at java.lang.reflect.Method.invokeNative(Native Method)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at java.lang.reflect.Method.invoke(Method.java:525)
03-11 09:43:55.958: E/AndroidRuntime(1518):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

Manifest 表现

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

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

    <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="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <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" >
        <uses-library android:name="com.google.android.maps" >
        </uses-library>

        <activity
            android:name="com.example.mapdemo2.MapDemo2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAfdExXel00usxddAR-WtSJ0FMcXzSMERA" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" >
        </meta-data>
    </application>

</manifest>

Activity XML 活动XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MapDemo2" >

    <fragment

        android:id="@+id/fragment1"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Java Code Java代码

package com.example.mapdemo2;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class MapDemo2 extends FragmentActivity {

    GoogleMap maps;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_demo2);
        SupportMapFragment mapFrag = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.fragment1));
        maps = mapFrag.getMap();
        maps.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }


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

}

XML XML格式

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MapDemo2" >

<fragment

    android:id="@+id/fragment1"
    class="com.google.android.gms.maps.MapFragment"   <--- use MapFragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

Java Code Java代码

    MapFragment mapFrag = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment1));
    maps = mapFrag.getMap();

Google introduced Google Play Services which has GoogleApiClient() to access maps and a lot of other features it support recently. Google推出了Google Play服务,该服务具有GoogleApiClient()来访问地图以及它最近支持的许多其他功能。 I would recommend you to add a reference to the Google Play Services library project to your app project. 我建议您将对Google Play服务库项目的引用添加到您的应用程序项目中。 Please see the following documentation to see how to make use of the Google Play Services using GoogleapiClient to access maps. 请参阅以下文档,以了解如何通过GoogleapiClient使用Google Play服务访问地图。

Please see this official documentation for code implementation. 请参阅此官方文档以了解代码实现。

Refer to this tutorial if you want to access Google Maps through Google Play Services. 如果您想通过Google Play服务访问Google Maps,请参考本教程

Hope that Helps!! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM