Galera, estou com dificuldade de criar uma window customizada e não consigo entender o motivo.
Ao clicar no marcador queria que mostrasse as informações de quando ele foi criado, como lat, long, vel, data e tals.
Logo que inicializa aplicação, se clico no marcador ele funciona normalmente, mas quando ando e ele gera um novo marcador, se clico nele o aplicativo fecha. Ao debugar, vi que a Exception pega no setMarker(), ao instanciar o LinearLayout.
public class MainActivity extends Activity implements View.OnClickListener {
private Location location;
private static GoogleMap map;
private static MarkerOptions markerOptions;
private static List<MarkerOptions> rotaExecutada;
private static MarkerOptions currentPosition;
private Button rota;
private static Button atual;
private static View viewButton;
private SimpleDateFormat dt = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss");
private Marker marker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeComponents();
location = LocationUtil.getLocation(this);
changeLocation(location);
}
public void initializeComponents() {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
rota = (Button) findViewById(R.id.btnRota);
atual = (Button) findViewById(R.id.btnLocalAtual);
rota.setOnClickListener(this);
atual.setOnClickListener(this);
}
public void changeLocation(Location location) {
if (location != null) {
Date date = new Date();
String dataFormat = dt.format(date);
String msg = "Latitude: " + location.getLatitude() + "\n" + "Longitude: " + location.getLongitude() + "\n"
+ "Velocidade: " + location.getSpeed() + "\n" + "Altitude: " + location.getAltitude() + "\n"
+ "Data: " + dataFormat;
LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude());
markerOptions = new MarkerOptions().position(currentPosition).title("Info: ").snippet(msg)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_foot));
map.moveCamera(CameraUpdateFactory.newLatLng(currentPosition));
if (MainActivity.currentPosition == null)
map.animateCamera(CameraUpdateFactory.zoomTo(18), 2000, null);
if (viewButton == atual || viewButton == null)
map.clear();
setMarker(markerOptions);
getRotaExecutada().add(markerOptions);
MainActivity.currentPosition = markerOptions;
}
}
public void setMarker(MarkerOptions markerOptions) {
marker = map.addMarker(markerOptions);
// map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
//
// @Override
// public void onInfoWindowClick(Marker marker) {
//
// if (marker.isInfoWindowShown()) {
// marker.hideInfoWindow();
// }
//
// }
// });
map.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
@Override
public View getInfoContents(Marker marker) {
LinearLayout ll = null;
try {
ll = new LinearLayout(MainActivity.this); // java.lang.NullPointerException: Attempt to invoke virtual method
// 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
ll.setPadding(20, 20, 20, 20);
ll.setBackgroundColor(Color.WHITE);
TextView tv = new TextView(MainActivity.this);
tv.setText(marker.getSnippet());
ll.addView(tv);
} catch (Exception e) {
Log.writeLog(true, e.getMessage());
}
return ll;
}
});
}
@Override
public void onClick(View view) {
MainActivity.viewButton = view;
if (view == findViewById(R.id.btnLocalAtual)) {
mostrarAtual();
} else if (view == findViewById(R.id.btnRota)) {
mostrarRota();
}
}
private void mostrarAtual() {
map.clear();
marker = map.addMarker(currentPosition);
map.moveCamera(CameraUpdateFactory.newLatLng(currentPosition.getPosition()));
map.animateCamera(CameraUpdateFactory.zoomTo(20), 2000, null);
}
private void mostrarRota() {
for (MarkerOptions markerOption : getRotaExecutada()) {
marker = map.addMarker(markerOption);
}
map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
public static List<MarkerOptions> getRotaExecutada() {
if (rotaExecutada == null)
rotaExecutada = new ArrayList<MarkerOptions>();
return rotaExecutada;
}
}