我正在尝试使用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();
}
}
的Manifest.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>
activity_map.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'
答案 0 :(得分:1)
您在行activity_map
中使用setContentView(R.layout.activity_map);
,但您的xml名称为activity_main
因此您收到错误。
使用setContentView(R.layout.activity_main);
并解决您的问题。