我想为地图设置监听器,例如Info Window click或Marker click,但是当使用setOnInfoWindowClickListener或setOnMarkerClickListener时,应用程序崩溃,如果我删除了监听器,应用程序正常启动,任何人都请帮助我,非常感谢!
package com.example.mapdemo;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import com.example.mapdemo.MyLocation.LocationResult;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
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 implements OnInfoWindowClickListener{
public GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//<cach 2>
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
//Got the location!
mMap = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
double dLatitude = location.getLatitude();
double dLongitude = location.getLongitude();
mMap.addMarker(new MarkerOptions().position(
new LatLng(dLatitude, dLongitude)).title("My Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_current_location)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 17));
addMarkers(mMap);
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
public View getInfoWindow(Marker arg0) {
View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
return v;
}
public View getInfoContents(Marker arg0) {
//View v = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
return null;
}
});
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
mMap.setOnInfoWindowClickListener(this);
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
Intent intent = new Intent(getBaseContext(),ThemTrangTraiActivity.class);
startActivity(intent);
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void viTriHienTai (View view) {
LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(Location location){
//Got the location!
mMap = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
mMap.clear();
double dLatitude = location.getLatitude();
double dLongitude = location.getLongitude();
mMap.addMarker(new MarkerOptions().position(
new LatLng(dLatitude, dLongitude)).title("My Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_current_location)));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dLatitude, dLongitude), 17));
addMarkers(mMap);
}
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
public void mapType (View view) {
mMap = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
if (mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL){
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
} else {
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
public void hienThiTabBarActivity (View view) {
Intent intent = new Intent(this, TabBarActivity.class);
startActivity(intent);
}
@Override
public void onInfoWindowClick(Marker arg0) {
Intent intent = new Intent();
intent.setClass(getBaseContext(), ThemTrangTraiActivity.class);
startActivity(intent);
}
public void addMarkers(GoogleMap mMap) {
//5 do
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.79055178, 106.6996908)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.789337, 106.7000939)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.7903673, 106.6994548)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.790025, 106.700367)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.790910, 106.699948)));
//5 vang
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.789442, 106.698694))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.789221, 106.698758))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.789094, 106.699187))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.788525, 106.699541))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.788222, 106.699194))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
//5 xanh la
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.789786, 106.701044))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.789311, 106.700936))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.788826, 106.700635))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.788763, 106.701493))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
mMap.addMarker(new MarkerOptions()
.position(new LatLng(10.788521, 106.701150))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@android:color/transparent"
android:orientation="vertical" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:onClick="viTriHienTai"
android:src="@drawable/ic_action_location_found" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:onClick="mapType"
android:src="@drawable/ic_action_map" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/background_light" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:onClick="hienThiTabBarActivity" />
</LinearLayout>
</FrameLayout>
logcat的
05-28 11:14:59.722: E/AndroidRuntime(2075): FATAL EXCEPTION: main
05-28 11:14:59.722: E/AndroidRuntime(2075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.MainActivity}: java.lang.NullPointerException
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.os.Looper.loop(Looper.java:137)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-28 11:14:59.722: E/AndroidRuntime(2075): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 11:14:59.722: E/AndroidRuntime(2075): at java.lang.reflect.Method.invoke(Method.java:511)
05-28 11:14:59.722: E/AndroidRuntime(2075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-28 11:14:59.722: E/AndroidRuntime(2075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-28 11:14:59.722: E/AndroidRuntime(2075): at dalvik.system.NativeStart.main(Native Method)
05-28 11:14:59.722: E/AndroidRuntime(2075): Caused by: java.lang.NullPointerException
05-28 11:14:59.722: E/AndroidRuntime(2075): at com.example.mapdemo.MainActivity.onCreate(MainActivity.java:66)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.Activity.performCreate(Activity.java:5008)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-28 11:14:59.722: E/AndroidRuntime(2075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-28 11:14:59.722: E/AndroidRuntime(2075): ... 11 more
答案 0 :(得分:0)
在LocationResult类之前初始化地图。这将修复空指针异常
mMap =((MapFragment)getFragmentManager()。findFragmentById(R.id.map))。getMap(); LocationResult locationResult = new LocationResult(){}