我正在尝试学习如何使用Google API V2开发谷歌地图Android应用。它在我没有添加
时有效private GoogleMap mMap = null;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
MyGoogleMapDemo.java 中的但是当我添加它们时,应用程序会继续崩溃。我正在使用
我还在我的项目中添加了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();
}
main.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>
log.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
答案 0 :(得分:5)
您的LogCat中可能还有与您的问题相关的其他消息。我的猜测是你的问题是你有一个格式错误的<fragment>
元素。你有:
<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"
... />
你不应该 android:name
和 class
,他们当然不应该指向不同的类。 android:name
是首选方法。由于您继承自FragmentActivity
,我猜您确实希望android:name
指向SupportMapFragment
,因此请进行更改并删除class
属性。您可能还希望在开始时删除xmlns:map
和所有map:
属性,直到您的其余代码正常工作。