InfoWindow错误

时间:2014-04-17 06:11:30

标签: android infowindow

这是我的MainMapView.java

public class MainMapView extends FragmentActivity{
    // Map and location
    private GoogleMap googleMap;
    private LocationManager locationManager;
    private LatLng latLng;
    private double latitude = 0;
    private double longitude = 0;
    private List<Address> addresses;
    private List<Address> markerAddresses;
    private Geocoder geocoder;
    private Marker marker;

    //Marker values 
    private TextView tvAddress;
    private TextView tvPostalCode;
    private TextView tvCity;
    private TextView tvCountry;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_view);
        tvAddress = (TextView)findViewById(R.id.infowindow_address);
        tvPostalCode = (TextView)findViewById(R.id.infowindow_postalcode);
        tvCity = (TextView)findViewById(R.id.infowindow_city);
        tvCountry = (TextView)findViewById(R.id.infowindow_country);
        //setup the map
        setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (googleMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        // Check if we were successful in obtaining the map.
        if (googleMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
        //Enable MyLocation layer of Google Map
        googleMap.setMyLocationEnabled(true);

        //Get LocationManager object from System Service LOCATION_SERVICE
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        //create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        //get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        //get current location
        Location myLocation = locationManager.getLastKnownLocation(provider);
        if(myLocation != null){
            latitude = myLocation.getLatitude();
            longitude = myLocation.getLongitude();
        }else{
            myLocation=locationManager.getLastKnownLocation("GPS");
            latitude = myLocation.getLatitude();
            longitude = myLocation.getLongitude();
        }

        //set map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);        

        //create a latingobject for the current location
        latLng =new LatLng(latitude, longitude);        

        //show the current location in google map
        googleMap.setMyLocationEnabled(true);
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        //zoom in the google map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        //Set InfoWindowAdapter
            googleMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());    
        //listen to long press on the map to make markers
        googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

            @Override
            public void onMapLongClick(LatLng arg0) {
                    // when getting the long click clear all marker on the map and add the new one
                    googleMap.clear();

                    // make sure to save the new marker latitude and longitude
                    latitude = arg0.latitude;
                    longitude = arg0.longitude;

                    // show the address
                    geocoder = new Geocoder(MainMapView.this, Locale.getDefault());
                    try {
                        addresses = geocoder.getFromLocation(latitude, longitude, 1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    // Adding marker on the GoogleMap
                    marker = googleMap.addMarker(new MarkerOptions().position(arg0));

                    // Showing InfoWindow on the GoogleMap
                    marker.showInfoWindow();
}

private class CustomInfoWindowAdapter implements InfoWindowAdapter{

    @Override
    public View getInfoWindow(Marker arg0) {
        return null;
    }

    @Override
    public View getInfoContents(Marker arg0) {
        View v = getLayoutInflater().inflate(R.layout.map_infowindow_style, null);

        // show the address
        geocoder = new Geocoder(MainMapView.this, Locale.getDefault());
        try {
            markerAddresses = geocoder.getFromLocation(arg0.getPosition().latitude, arg0.getPosition().longitude, 1);
        } catch (IOException e) {
            e.getCause();
        }
        tvAddress.setText(arg0.getTitle());
        tvPostalCode.setText(arg0.getSnippet());
        tvCity.setText(arg0.getSnippet());
        tvCountry.setText(arg0.getSnippet());

        v.setLayoutParams(new ViewGroup.LayoutParams(v.getWidth(),v.getHeight()));
        return v;
    }


}
}

map_infowindow_style.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/infowindow_style_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:orientation="vertical" >

    <TextView
        android:id="@+id/infowindow_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/infowindow_postalcode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/infowindow_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/infowindow_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:id="@+id/vertical_line"
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@color/DarkGray" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="?android:attr/selectableItemBackground"
        android:text="Add" />

</LinearLayout>

这是我得到的错误,我无法找到解决方案!!?

04-15 19:32:30.937: D/AndroidRuntime(13855): Shutting down VM
04-15 19:32:30.937: W/dalvikvm(13855): threadid=1: thread exiting with uncaught exception (group=0x419687c0)
04-15 19:32:30.947: E/AndroidRuntime(13855): FATAL EXCEPTION: main
04-15 19:32:30.947: E/AndroidRuntime(13855): java.lang.NullPointerException
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.nyanrex52.partylandv2.MainMapView$CustomInfoWindowAdapter.getInfoContents(MainMapView.java:314)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.google.android.gms.maps.GoogleMap$11.g(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at android.os.Binder.transact(Binder.java:347)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at etp.b(SourceFile:112)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.e.bm.a(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.e.bm.b(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.e.bn.g(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at ewp.onTransact(SourceFile:145)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at android.os.Binder.transact(Binder.java:347)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.google.android.gms.maps.model.internal.d$a$a.showInfoWindow(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.google.android.gms.maps.model.Marker.showInfoWindow(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.nyanrex52.partylandv2.MainMapView$2.onMapLongClick(MainMapView.java:198)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.google.android.gms.maps.GoogleMap$7.onMapLongClick(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.google.android.gms.maps.internal.j$a.onTransact(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at android.os.Binder.transact(Binder.java:347)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at euq.a(SourceFile:93)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.c.j.b(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.ay.an.c(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.ay.bc.onLongPress(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.bo.g.onLongPress(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.bo.h.c(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at maps.bo.i.handleMessage(Unknown Source)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at android.os.Looper.loop(Looper.java:137)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at android.app.ActivityThread.main(ActivityThread.java:5289)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at java.lang.reflect.Method.invoke(Method.java:525)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-15 19:32:30.947: E/AndroidRuntime(13855):    at dalvik.system.NativeStart.main(Native Method)

另外,我想知道是否有人有一个非常好的教程解释&#34; Custom Infowindow&#34;。我之所以问的是,到目前为止我所看到的所有教程都在这里,他们都使用getTitle和getSnippet在infowindow中描述了2行。我不明白如何用更多的线来做到这一点?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。问题出在班级CustomInfoWindowAdapter

我所要做的就是在这个类的开头初始化View,这样两种方法都可以使用它。

我花了很长时间才看到它但是,我做到了:)

private class CustomInfoWindowAdapter implements InfoWindowAdapter{
    View v = getLayoutInflater().inflate(R.layout.map_infowindow_style, null);
    @Override
    public View getInfoWindow(Marker arg0) {
        return v;

    }

    @Override
    public View getInfoContents(Marker arg0) {
        tvAddress = (TextView)findViewById(R.id.infowindow_address);
        tvPostalCode = (TextView)findViewById(R.id.infowindow_postalcode);
        tvCity = (TextView)findViewById(R.id.infowindow_city);
        tvCountry = (TextView)findViewById(R.id.infowindow_country);


        // show the address
        geocoder = new Geocoder(MainMapView.this, Locale.getDefault());
        try {
            markerAddresses = geocoder.getFromLocation(arg0.getPosition().latitude, arg0.getPosition().longitude, 1);
        } catch (IOException e) {
            e.getCause();
        }
        tvAddress.setText(arg0.getTitle());
        tvPostalCode.setText(arg0.getSnippet());
        tvCity.setText(arg0.getSnippet());
        tvCountry.setText(arg0.getSnippet());

        v.setLayoutParams(new ViewGroup.LayoutParams(v.getWidth(),v.getHeight()));
        return v;
    }

}