自定义信息窗口崩溃应用程序试图膨胀imageView

时间:2016-01-02 11:55:01

标签: android imageview google-maps-android-api-2

尝试在我的某个标记上打开窗口时,我收到了以下错误

01-02 13:39:38.001 22174-22174/com.avishaycohen.petahtikvatour_followthefounders E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                   Process: com.avishaycohen.petahtikvatour_followthefounders, PID: 22174
                                                                                                   android.view.InflateException: Binary XML file line #8: Error inflating class imageView
                                                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
                                                                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
       ... 60 more
                                                                                                    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

这是我的主要活动代码:

package com.avishaycohen.petahtikvatour_followthefounders;

import android.content.res.TypedArray;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

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.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
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;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    //the map
    private GoogleMap mMap;
    private HashMap markers;
    // starting zoom value
    public static final float START_ZOOM = 16;
    //center of town
    public static final LatLng CENTER = new LatLng(32.087948,34.883956);
    //egde of city
    //public static final LatLng NORTH_EAST_EDGE = new LatLng(32.107821,34.900732);
    //public static final LatLng SOUTH_WEST_EDGE = new LatLng(32.073354,34.859619);

    // Create a LatLngBounds that includes only Petah tikva.
    //public static final LatLngBounds PETAH_TIKVA = new LatLngBounds(SOUTH_WEST_EDGE, NORTH_EAST_EDGE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        markers = new HashMap();
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;

        // Setting a custom info window adapter for the google map
        googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker arg0) {

                // Getting view from the layout file info_window_layout
                View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);
                TypedArray imgs = getResources().obtainTypedArray(R.array.locations_img);
                // Getting the position from the marker
                LatLng latLng = arg0.getPosition();

                // Getting reference to the TextView to set title
                TextView tvTitle = (TextView) v.findViewById(R.id.tv_title);
                // Getting reference to the TextView to set info
                TextView tvInfo = (TextView) v.findViewById(R.id.tv_info);
                // getting reference to the ImageView to set the img for location
                ImageView ivImage = (ImageView) v.findViewById(R.id.iv_image);
                // Setting the title
                tvTitle.setText(arg0.getTitle());
                // Setting the info
                tvInfo.setText(arg0.getSnippet());
                // Setting the image
                ivImage.setImageResource((int)markers.get(arg0.getId()));
                // Returning the view containing InfoWindow contents
                return v;
            }
        });

        //the following will hold the coordinates for the points of inerest in the city
        ArrayList<LatLng> interestPoints = new ArrayList<LatLng>();

        //hold the north values of the coordinate map
        double[] northPoints = new double[]{
                32.089483,
                32.088805,
                32.086431,
                32.085204,
                32.084938,
                32.086554,
                32.087391,
                32.087582,
                32.087987,
                32.089743,
                32.089344,
                32.089075,
                32.087962,
                32.087566,
                32.087844,
                32.090166
        };

        //hold the east values of the coordinate map
        double[] eastPoints = new double[]{
                34.88095,
                34.880871,
                34.881796,
                34.88371,
                34.885445,
                34.886906,
                34.887457,
                34.887537,
                34.887127,
                34.885697,
                34.885375,
                34.884646,
                34.882902,
                34.883288,
                34.882317,
                34.881963,
        };

        //get the names of all the points to load them into the map
        String[] pointsNames = getResources().getStringArray(R.array.locations_names);
        String[] pointsInfo = getResources().getStringArray(R.array.locations_snippets);
        TypedArray imgs;
        imgs = getResources().obtainTypedArray(R.array.locations_img);
        // just to have how many points i've stored
        int size = northPoints.length;
        //iterate over the points create them as data and create them as markers
        for (int i = 0; i < size; i++) {
            interestPoints.add(i, new LatLng(northPoints[i], eastPoints[i]));
            Marker currMarker = mMap.addMarker(new MarkerOptions()
                    .position(interestPoints.get(i))
                    .alpha(0.7f)
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
                    .snippet(pointsInfo[i])
                    .title(pointsNames[i]));
            markers.put(currMarker.getId(),imgs.getResourceId(i, 0));
        }

        //move the camera to the center of PT city and zoom in
        mMap.moveCamera(CameraUpdateFactory.zoomTo(START_ZOOM));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(CENTER));

        /**
         * this is not good coding - i'm bypassing google's permission policy
         * assuming i'll not have any users using Android 6 or above (M or later)
         * so it works only between 4.X.X to 5.X.X
         */
        mMap.setMyLocationEnabled(true);

        /**
         * This is me trying to enforce boundries and failing, need to understand better...
         * ****************************************************************************************
         * Set the camera to the greatest possible zoom level that includes the                   *
         * bounds                                                                                 *
         * mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(PETAH_TIKVA, (int) START_ZOOM));   *
         * ****************************************************************************************
         */

    }
}

这是我自定义窗口的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/bg_color" >

    <imageView
        android:id="@+id/iv_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:adjustViewBounds="true"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

我已经关注了网上的几个指南,并且还阅读了一些关于SO的相关问题,如果我删除了imageView以及与之相关的所有内容,代码就可以工作了,我完全得到了标题和片段,但我可以'使它适用于图像。

对于标记我拥有存储在XML数组中的所有信息,它们的名称,片段和可绘制的res。

欢迎任何帮助。

1 个答案:

答案 0 :(得分:2)

没有名为imageView的控件ImageView,更改xml

<ImageView
    android:id="@+id/iv_image"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:adjustViewBounds="true"
    />