Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 信息窗口错误_Android_Infowindow - Fatal编程技术网

Android 信息窗口错误

Android 信息窗口错误,android,infowindow,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 doub

这是我的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;
    }


}
}

另外,我想知道是否有人有一个很好的教程来解释“自定义信息窗口”。我问这个问题的原因是,到目前为止,我在这里看到的所有教程都使用getTitle和getSnippet在infowindow中描述了两行代码。我不明白怎么用更多的线来做

我找到了解决办法。问题在于我的类中的视图
CustomInfoWindowAdapter

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

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


MainMapView类中314号的哪一行?//设置InfoWindowAdapter googleMap.setInfoWindowAdapter(新的CustomInfoWindowAdapter());首先检查googleMap是否为null,然后设置适配器。您会看到“setupMapifNeedd()”方法。。。。。这就是它的作用。。。它确保Google地图在使用前不为空。您的问题不是关于信息窗口,而是关于您的Google的问题没有正确初始化,所以请确保您的Google\u lay\u服务库
<?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)
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;
    }

}