0

我正在尝试使用 Google Maps API v2 构建一个应用程序,我按照developers.android.com 上的说明进行操作,但它仍然给我错误请帮助我

这是main.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.example.googlemap.R;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MapActivity extends FragmentActivity{

SupportMapFragment mMap;
GoogleMap googleMap;

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

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

    googleMap = mMap.getMap();
 }
}

清单.xml:

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

    <permission
        android:name="com.example.googlemap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="com.example.googlemap.permission.MAPS_RECEIVE"/>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission      android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <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="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.googlemap.MapActivity"
            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="api_key"/>// i wrote it
    </application>

</manifest>

活动地图.xml

<RelativeLayout 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"
    tools:context=".MapActivity" >

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

</RelativeLayout>

最后我的logcat:

11-04 11:15:56.034: D/AndroidRuntime(868): Shutting down VM
11-04 11:15:56.074: W/dalvikvm(868): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
11-04 11:15:56.154: E/AndroidRuntime(868): FATAL EXCEPTION: main
11-04 11:15:56.154: E/AndroidRuntime(868): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.googlemap/com.example.googlemap.MapActivity}: java.lang.ClassNotFoundException: com.example.googlemap.MapActivity
11-04 11:15:56.154: E/AndroidRuntime(868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
11-04 11:15:56.154: E/AndroidRuntime(868):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-04 11:15:56.154: E/AndroidRuntime(868):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-04 11:15:56.154: E/AndroidRuntime(868):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-04 11:15:56.154: E/AndroidRuntime(868):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:15:56.154: E/AndroidRuntime(868):  at android.os.Looper.loop(Looper.java:137)
11-04 11:15:56.154: E/AndroidRuntime(868):  at android.app.ActivityThread.main(ActivityThread.java:4424)
11-04 11:15:56.154: E/AndroidRuntime(868):  at java.lang.reflect.Method.invokeNative(Native Method)
11-04 11:15:56.154: E/AndroidRuntime(868):  at java.lang.reflect.Method.invoke(Method.java:511)
11-04 11:15:56.154: E/AndroidRuntime(868):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-04 11:15:56.154: E/AndroidRuntime(868):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-04 11:15:56.154: E/AndroidRuntime(868):  at dalvik.system.NativeStart.main(Native Method)
11-04 11:15:56.154: E/AndroidRuntime(868): Caused by: java.lang.ClassNotFoundException: com.example.googlemap.MapActivity
11-04 11:15:56.154: E/AndroidRuntime(868):at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-04 11:15:56.154: E/AndroidRuntime(868):at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-04 11:15:56.154: E/AndroidRuntime(868):at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-04 11:15:56.154: E/AndroidRuntime(868):at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
11-04 11:15:56.154: E/AndroidRuntime(868): at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
11-04 11:15:56.154: E/AndroidRuntime(868): 11 more
11-04 11:15:56.256: I/dalvikvm(868): threadid=3: reacting to signal 3
11-04 11:15:56.484: I/dalvikvm(868): Wrote stack traces to '/data/anr/traces.txt'
11-04 11:15:56.844: I/dalvikvm(868): threadid=3: reacting to signal 3
11-04 11:15:56.854: I/dalvikvm(868): Wrote stack traces to '/data/anr/traces.txt'
11-04 11:15:57.274: I/dalvikvm(868): threadid=3: reacting to signal 3
11-04 11:15:57.324: I/dalvikvm(868): Wrote stack traces to '/data/anr/traces.txt'
4

1 回答 1

1

你使用 activity_mapin line setContentView(R.layout.activity_map);但你的 xml 名称是activity_main所以你得到了错误。

使用setContentView(R.layout.activity_main);并解决您的问题。

于 2013-11-04T11:35:12.450 回答