在Android中使用Google Map V2时出现ClassNotFoundException

时间:2015-03-11 14:08:24

标签: java android eclipse google-maps google-maps-android-api-2

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)

清单

<?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>

活动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代码

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;
    }

}

2 个答案:

答案 0 :(得分:0)

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代码

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

答案 1 :(得分:0)

Google推出了Google Play服务,其中包含GoogleApiClient(),可以访问地图以及最近支持的许多其他功能。我建议您将Google Play服务库项目的引用添加到您的应用项目中。请参阅以下documentation,了解如何使用Google PlayiClient访问地图来使用Google Play服务。

请参阅此官方documentation以了解代码实施情况。

如果您想通过Google Play服务访问Google地图,请参阅此tutorial

希望有助于!!