我正在尝试学习如何使用 Google API V2 开发谷歌地图 Android 应用程序。当我没有添加
private GoogleMap mMap = null;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
在MyGoogleMapDemo.java 但是当我添加它们时,应用程序不断崩溃。我正在使用
- 谷歌 API 4.2
- 安卓 SDK 工具 21.1
- Android SDK平台-工具16.0.1
- Eclipse SDK 3.7.1
而且我还将 android-support-v4.jar 添加到我的项目中。我期待着
MyGoogleMapDemo.java
package org.lxh.demo;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MyGoogleMapDemo extends FragmentActivity
{
private GoogleMap mMap = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
map:cameraBearing="112.5"
map:cameraTargetLat="-33.796923"
map:cameraTargetLng="150.922433"
map:cameraTilt="30"
map:cameraZoom="13"
map:mapType="normal"
map:uiCompass="true"
map:uiRotateGestures="true"
map:uiScrollGestures="true"
map:uiTiltGestures="true"
map:uiZoomControls="false"
map:uiZoomGestures="true" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<permission
android:name="org.lxh.demo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="org.lxh.demo.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" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCHCxLLeMk3zRL4TYICdbYjB6nQk-oCvRs" />
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MyGoogleMapDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
日志.txt
02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapUtilization:0.25
02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapIdealFree:8388608
02-17 22:56:51.954: D/ActivityThread(11229): setTargetHeapConcurrentStart:2097152
02-17 22:56:52.585: W/dalvikvm(11229): threadid=1: thread exiting with uncaught exception (group=0x4106d498)
02-17 22:56:52.585: E/AndroidRuntime(11229): FATAL EXCEPTION: main
02-17 22:56:52.585: E/AndroidRuntime(11229): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.lxh.demo/org.lxh.demo.MyGoogleMapDemo}: java.lang.NullPointerException
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.access$600(ActivityThread.java:136)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.os.Looper.loop(Looper.java:137)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.main(ActivityThread.java:4797)
02-17 22:56:52.585: E/AndroidRuntime(11229): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 22:56:52.585: E/AndroidRuntime(11229): at java.lang.reflect.Method.invoke(Method.java:511)
02-17 22:56:52.585: E/AndroidRuntime(11229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
02-17 22:56:52.585: E/AndroidRuntime(11229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
02-17 22:56:52.585: E/AndroidRuntime(11229): at dalvik.system.NativeStart.main(Native Method)
02-17 22:56:52.585: E/AndroidRuntime(11229): Caused by: java.lang.NullPointerException
02-17 22:56:52.585: E/AndroidRuntime(11229): at org.lxh.demo.MyGoogleMapDemo.onCreate(MyGoogleMapDemo.java:24)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.Activity.performCreate(Activity.java:5024)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-17 22:56:52.585: E/AndroidRuntime(11229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
02-17 22:56:52.585: E/AndroidRuntime(11229): ... 11 more