这是我的主要课程
package com.example.mapv2example;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MyActivity extends FragmentActivity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapv2example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.example.mapv2example.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.mapv2example.maps.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/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.example.mapv2example.MyActivity"
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="MY_API_Key" />
</application>
</manifest>
和main.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=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
我在IntellijIdea 12上工作,我添加了android支持库和谷歌播放服务,但当我尝试运行我的应用程序模拟器“不幸的是,mapv2example已停止”我从谷歌获取地图版本2 api密钥也是我的错误日志
12-26 09:03:12.852: ERROR/AndroidRuntime(1714): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapv2example/com.example.mapv2example.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.mapv2example.MyActivity.onCreate(MyActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
... 11 more
12-26 09:03:12.996: WARN/ActivityManager(298): Force finishing activity com.example.mapv2example/.MyActivity
12-26 09:03:13.043: WARN/WindowManager(298): Screenshot failure taking screenshot for (328x583) to layer 22010
12-26 09:03:13.652: WARN/ActivityManager(298): Activity pause timeout for ActivityRecord{41896338 u0 com.example.mapv2example/.MyActivity}
12-26 09:03:24.261: WARN/ActivityManager(298): Activity destroy timeout for ActivityRecord{41896338 u0 com.example.mapv2example/.MyActivity}
12-26 09:08:13.061: INFO/Process(1714): Sending signal. PID: 1714 SIG: 9
12-26 09:08:13.071: INFO/ActivityManager(298): Process com.example.mapv2example (pid 1714) has died.
12-26 09:08:13.111: WARN/InputMethodManagerService(298): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41c4dd88 attribute=null, token = android.os.BinderProxy@41b7fd80
12-26 09:10:33.132: DEBUG/dalvikvm(435): GC_FOR_ALLOC freed 511K, 19% free 3041K/3752K, paused 540ms, total 540ms
12-26 09:20:33.401: INFO/ActivityManager(298): No longer want com.android.exchange (pid 1420): empty for 1804s
12-26 09:21:33.782: INFO/ActivityManager(298): No longer want com.android.browser (pid 1511): empty for 1810s
12-26 09:21:33.844: INFO/ActivityManager(298): No longer want com.android.calendar (pid 1495): empty for 1811s
12-26 09:21:33.897: INFO/ActivityManager(298): No longer want com.android.sharedstoragebackup (pid 1531): empty for 1809s
12-26 09:21:41.868: DEBUG/GCM(490): Ignoring attempt to send heartbeat on dead connection.
12-26 09:23:08.892: DEBUG/dalvikvm(298): GC_FOR_ALLOC freed 1142K, 38% free 7504K/12064K, paused 113ms, total 128ms
12-26 09:26:45.921: INFO/EventLogService(490): Aggregate from 1388066205680 (log), 1388066205680 (data)
答案 0 :(得分:0)
你是否在build.gradle中正确编译了google play服务...你可以按照android开发者网站上的步骤... http://developer.android.com/google/play-services/setup.html ...也许你将不得不使用gmaps api v3 ......
...问候!!