我正在尝试使用 Google maps v2 运行应用程序,但 Logcat 显示错误:
致命异常:主要 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.app.mapa/com.app.MapaLugares}:android.view.InflatException:java.lang.NullPointerException
这是代码:
activity_mapa_lugares.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MapaLugares.java
package com.app.mapa;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Point;
import android.view.Menu;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.model.LatLng;
public class MapaLugares extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa_lugares);
mMap = ((MapFragment) getFragmentManager().findFragmentById (R.id.mapa)).
getMap();
mMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
Projection proj = mMap.getProjection();
Point coord = proj.toScreenLocation(point);
Toast.makeText(MapaLugares.this, "Click\n" + "Lat: " +
point.latitude + "\n" + "Lng: "+ point.longitude+ "\n" + "X: "
+ coord.x + " - Y: "+ coord.y, Toast.LENGTH_SHORT)
.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mapa_lugares, menu);
return true;
}
}
怎么了?
谢谢。