Java 类未找到google maps api v2和eclipse异常

Java 类未找到google maps api v2和eclipse异常,java,android,eclipse,google-maps,Java,Android,Eclipse,Google Maps,我想写一个简单的地图程序。我正在使用eclipse。我得到以下例外情况: 05-06 10:25:33.474: E/AndroidRuntime(7341): FATAL EXCEPTION: main 05-06 10:25:33.474: E/AndroidRuntime(7341): Process: com.example.mapapp, PID: 7341 05-06 10:25:33.474: E/AndroidRuntime(7341): java.lang.RuntimeExc

我想写一个简单的地图程序。我正在使用eclipse。我得到以下例外情况:

05-06 10:25:33.474: E/AndroidRuntime(7341): FATAL EXCEPTION: main
05-06 10:25:33.474: E/AndroidRuntime(7341): Process: com.example.mapapp, PID: 7341
05-06 10:25:33.474: E/AndroidRuntime(7341): java.lang.RuntimeException: Unable to start          activity ComponentInfo{com.example.mapapp/com.example.mapapp.MapAppHome}:   android.view.InflateException: Binary XML file line #12: Error inflating class fragment
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.os.Looper.loop(Looper.java:136)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at java.lang.reflect.Method.invoke(Method.java:515)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at dalvik.system.NativeStart.main(Native Method)
05-06 10:25:33.474: E/AndroidRuntime(7341): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.Activity.setContentView(Activity.java:1929)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at com.example.mapapp.MapAppHome.onCreate(MapAppHome.java:13)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.Activity.performCreate(Activity.java:5231)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-06 10:25:33.474: E/AndroidRuntime(7341):     ... 11 more
05-06 10:25:33.474: E/AndroidRuntime(7341): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment   com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an   empty constructor that is public
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.Fragment.instantiate(Fragment.java:597)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.Fragment.instantiate(Fragment.java:561)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.Activity.onCreateView(Activity.java:4778)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
05-06 10:25:33.474: E/AndroidRuntime(7341):     ... 21 more
05-06 10:25:33.474: E/AndroidRuntime(7341): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.example.mapapp-2.apk"],nativeLibraryDirectories=[/data/app-  lib/com.example.mapapp-2, /vendor/lib, /system/lib]]
05-06 10:25:33.474: E/AndroidRuntime(7341):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
05-06 10:25:33.474: E/AndroidRuntime(7341):     at android.app.Fragment.instantiate(Fragment.java:583)
05-06 10:25:33.474: E/AndroidRuntime(7341):     ... 24 more
以下是我的android清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mapapp"
    android:versionCode="1"
    android:versionName="1.0" >
    <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"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<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"/>
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.mapapp.MapAppHome"
        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.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="my_code"/>
</application>
google play services库位于eclipse的工作区中,并被引用(到mapapp属性),它是否在工作区文件夹中并不重要(相同的错误)。 我有一个有效的api密钥,这次我在android studio的同一个项目中使用了它,它工作得非常好。 项目构建api设置为Google api 4.4.2,在项目属性中的java构建路径中的“顺序和导出”选项卡中,选中所有选项(Google api、android专用库和android依赖项)。 正如我之前所说的,同样的代码加上必要的(grandle)修改,效果非常好。我所有的sdk文件都是最新的

我错过了什么

使用FragmentActivity扩展您的活动
Extend your activity with FragmentActivity

    public class MapAppHome extends FragmentActivity {
    GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initalization();
        createMapIfReady();
    }

    @Override
    protected void onResume() {
        super.onResume();
        createMapIfReady();

    }

    private void createMapIfReady() {
        // TODO Auto-generated method stub

        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mMap = mapFragment.getMap();
        }

    }

    public void initalization() {

        int status = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getBaseContext());

        if (status == ConnectionResult.SUCCESS) {

            SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);

            mMap = supportMapFragment.getMap();



        }

        else {

            int requestCode = 10;

            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
                    requestCode);

            dialog.show();

        }
    }



and in you xml layout 

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

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

</LinearLayout>
公共类MapAppHome扩展了FragmentActivity{ 谷歌地图; @凌驾 创建时受保护的void(Bundle savedInstanceState){ //TODO自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); 初始化(); createMapIfReady(); } @凌驾 受保护的void onResume(){ super.onResume(); createMapIfReady(); } 私有void createMapifrady(){ //TODO自动生成的方法存根 如果(mMap==null){ //尝试从SupportMapFragment获取映射。 SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.map); mMap=mapFragment.getMap(); } } 公共无效初始化(){ int status=GooglePlayServicesUtil .isGooglePlayServicesAvailable(getBaseContext()); 如果(状态==ConnectionResult.SUCCESS){ SupportMapFragment SupportMapFragment=(SupportMapFragment)getSupportFragmentManager() .findFragmentById(R.id.map); mMap=supportMapFragment.getMap(); } 否则{ int requestCode=10; Dialog Dialog=GooglePlayServicesUtil.getErrorDialog(状态,此, 请求代码); dialog.show(); } } 在您的xml布局中
extend fragmentActivity相同的结果和错误。
package com.example.mapapp;

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

public class MapAppHome extends Activity {

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


@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_app_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}
Extend your activity with FragmentActivity

    public class MapAppHome extends FragmentActivity {
    GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initalization();
        createMapIfReady();
    }

    @Override
    protected void onResume() {
        super.onResume();
        createMapIfReady();

    }

    private void createMapIfReady() {
        // TODO Auto-generated method stub

        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mMap = mapFragment.getMap();
        }

    }

    public void initalization() {

        int status = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getBaseContext());

        if (status == ConnectionResult.SUCCESS) {

            SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);

            mMap = supportMapFragment.getMap();



        }

        else {

            int requestCode = 10;

            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
                    requestCode);

            dialog.show();

        }
    }



and in you xml layout 

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

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

</LinearLayout>