我是Android的新用户,我在http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html#googlemaps_project
按照Google Maps API教程进行操作我一直收到这个错误:
04-27 16:58:37.778: E/AndroidRuntime(5968): FATAL EXCEPTION: main
04-27 16:58:37.778: E/AndroidRuntime(5968): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.example.mapsapi/com.example.mapsapi.MainActivity}:
java.lang.NullPointerException
这是我的MainActivity.java:
package com.example.mapsapi;
import android.app.Activity;
import android.os.Bundle;
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 MainActivity extends Activity {
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.activity_main);
if(map == null){
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
}
if(map!=null)
map.setMyLocationEnabled(true);
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;
}
}
这是我的activity_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" >
</RelativeLayout>
这是我的Manifest XML文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapsapi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<permission
android:name="com.example.mapsapi.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.mapsapi.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"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mapsapi.MainActivity"
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.AIzaSyAVbioYpY53ofxwxU6KSNxn_yZoQ28cFZA"
android:value="AIzaSyAVbioYpY53ofxwxU6KSNxn_yZoQ28cFZA" />
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
有谁知道如何解决这个问题?当我在我的设备上运行它时,该应用程序说&#34;不幸的是,MapsAPI已停止。&#34;提前谢谢!
答案 0 :(得分:0)
当我在4个月前使用google maps API时,我记得有一个与getMap()
方法类似的问题,它并不总是返回地图。
if(map == null){
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
如果我没记错,上面的行不保证会返回引用,但它也可以返回null。
我的建议是将其余代码放在逻辑子句中,如下所示:
if(map!=null) {
map.setMyLocationEnabled(true);
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);
}
此外,要确认,您可以添加断点或打印一些Log
以确定代码的这部分是否正在执行,因为地图为null
。
答案 1 :(得分:0)
google示例中的MapFragment已过时..
而不是使用mapFragment使用supportMapFragment
示例:
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
并在XML中更改片段:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
答案 2 :(得分:0)
更改
<meta-data
android:name="com.google.android.maps.v2.AIzaSyAVbioYpY53ofxwxU6KSNxn_yZoQ28cFZA"
android:value="AIzaSyAVbioYpY53ofxwxU6KSNxn_yZoQ28cFZA" />
到
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAVbioYpY53ofxwxU6KSNxn_yZoQ28cFZA" />