IllegalStateException:填充后视图大小太小 [英] IllegalStateException: View size is too small after padding

查看:83
本文介绍了IllegalStateException:填充后视图大小太小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用程序。在这个应用程序中,我在片段中显示MapView。如果我的设备已旋转,并且我尝试使用以下方法之一移动相机:

mapa.animateCamera(cameraUpdate);
$或$
$ b $ p $ ma $ m $ mapa.moveCamera(cameraUpdate); / code>



然后我的应用程序崩溃并显示以下错误:

  java.lang.IllegalStateException:在填充com.google.aaab.b(未知源代码)的
后,com.google.maps.api.android.lib6中的
填充后的视图大小太小.gmm6.caa(未知来源)
,位于com.google.maps.api.android.lib6.gmm6.caa(未知来源)
,位于com.google.maps.api.android.lib6.d .dw.a(未知来源)
在com.google.maps.api.android.lib6.gmm6.caa(未知来源)
来自com.google.maps.api.android.lib6.d .et.a(Unknown Source)
at com.google.android.gms.maps.internal.j.onTransact(SourceFile:83)
at android.os.Binder.transact(Binder.java: 380)
,位于com.google.android.gms.maps.internal.IGoogleMapDelegate $ zza $ zza.moveCamera(Unkn自己的源)
在com.google.android.gms.maps.GoogleMap.moveCamera(未知源)
在com.accionplus.dbdplus.utils.MapUtils.centrarCamara(MapUtils.java:88)
at com.accionplus.dbdplus.utils.MapUtils.centrarCamara(MapUtils.java:52)
at com.accionplus.dbdplus.controladores.pdv.RutaProgramadaFragment $ 4.onMyLocationButtonClick(RutaProgramadaFragment.java:274)
at com.google.android.gms.maps.GoogleMap $ 3.onMyLocationButtonClick(Unknown Source)
at com.google.android.gms.maps.internal.zzo $ zza.onTransact(Unknown Source)
在android.os.Binder.transact(Binder.java:380)
在com.google.android.gms.maps.internal.be.a(SourceFile:81)
在com.google.maps .api.android.lib6.d.ak.onClick(未知来源)
at android.view.View.performClick(View.java:5156)
at android.view.View $ PerformClick.run( View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android .os.Loope r.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
在java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com。 android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

我已经使用了它,但是没有成功。我刚刚发现这是谷歌的一个报告问题,但我不知道是否有任何解决方法来解决这个问题。任何人都可以帮助我?



编辑:我添加了更多的代码

 < RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android。 com / tools
android:layout_width =match_parent
android:layout_height =match_parent
tools:context =com.accionplus.dbdplus.controladores.pdv.RutaProgramadaActivity>

< LinearLayout
android:id =@ + id / card_container
android:layout_width =match_parent
android:layout_height =wrap_content
android:background =#15000000
android:orientation =vertical/>

< LinearLayout
android:id =@ + id / filtro_ll
android:layout_width =match_parent
android:layout_height =wrap_content
android:layout_below =@ + id / card_container
android:layout_margin =10dp
android:gravity =left | top
android:orientation =horizo​​ntal
android:weightSum =1>

< LinearLayout
android:layout_width =0dp
android:layout_height =match_parent
android:layout_weight =0.7
android: gravity =left | top
android:orientation =vertical>

< LinearLayout
android:layout_width =match_parent
android:layout_height =wrap_content
android:gravity =left
android: orientation =horizo​​ntal
android:weightSum =1>

android:id =@ + id / seleccionar_fecha_btn
android:layout_width =wrap_content
android:layout_height =35dp
android:background =@ drawable / button
android:src =@ drawable / ic_date
android:text =@ string / buscar
android:textColor =@彩色/ color_text_primary/>

< TextView
android:id =@ + id / fecha_consulta_tv
android:layout_width =match_parent
android:layout_height =35dp
android:layout_alignParentTop =true
android:layout_marginLeft =5dp
android:background =@ drawable / rectangle
android:gravity =center/>
< / LinearLayout>

< LinearLayout
android:layout_width =match_parent
android:layout_height =wrap_content
android:layout_marginTop =10dp
android:背景= @绘制/矩形 >

< Spinner
android:id =@ + id / tipo_ruta_sp
android:layout_width =fill_parent
android:layout_height =35dp
android:layout_gravity =center
/>
< / LinearLayout>

< / LinearLayout>
< / LinearLayout>

< com.google.android.gms.maps.MapView
android:id =@ + id / map
android:layout_width =match_parent
android:layout_height =match_parent
android:layout_below =@ + id / filtro_ll/>

< ProgressBar
android:id =@ + id / progress_bar
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_centerHorizo​​ntal =true
android:foregroundGravity =center | top
android:visibility =gone/>

< / RelativeLayout>

以下是点击 MyLocationButton 时的情况: p>

  mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener(){
@Override
public boolean onMyLocationButtonClick(){
MapUtils.centrarCamara(getActivity(),mMap,mPuntosFinales);
return true;
}
});

以下是 MapUtils.centrarCamara()方法:

  public static void centrarCamara(Contextquxto,GoogleMap mapa,LatLng ... posiciones){
centrarCamara(contexto, mapa,Boolean.TRUE,posiciones);
}

public static void centrarCamara(Context maxt,boolean animado,
LatLng ... posiciones){
if(mapa == null){
return;
}

LatLngBounds.Builder builder = new LatLngBounds.Builder(); (LatLng posicion:posiciones)
{
builder.include(posicion);
}

LatLngBounds bounds = builder.build();

CameraUpdate cu = CameraUpdateFactory
.newLatLngBounds(bounds,ViewUtils.dpAPixeles(MARKERS_DP_OFFSET,contexto));

if(animado){
mapa.animateCamera(cu);
} else {
mapa.moveCamera(cu);


ViewUtils.dpAPixeles:

  public static int dpAPixeles(int dp,Context contexto){
Resources r = contexto.getResources();
int px =(int)TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,r.getDisplayMetrics());

return px;

$ / code>

mapa.moveCamera() mapa.animateCamera()被调用;

解决方案

通过文档


请勿更换相机更新,直到地图具有经过布局的
(为了使此方法正确地确定
适当的边界框和缩放级别,地图必须具有大小)。
否则会抛出IllegalStateException异常。它不是
足以让地图可用(即getMap()返回一个
非空对象);包含地图的视图也必须经过
布局,以确定其尺寸。如果您不能确定是否发生了这种情况,请改为使用newLatLngBounds(LatLngBounds,int,
int,int),并手动提供地图的尺寸​​。



因此,你的问题可能是当你调用 mapa.animateCamera(cameraUpdate) mapa.moveCamera(cameraUpdate)



无论如何,正如您所说,旋转时引发异常您的设备,我会说真正的问题是您调用 newLatLngBounds ViewUtils.dpAPixeles(MARKERS_DP_OFFSET,contexto))导致问题(也许填充大于视图的高度/ 2?)。有一个已注册的问题( 4773 )相关Google地图可以帮助您解决问题。如上所述,一种可能的解决方法是根据方向设置cameraUpdate:

  if(getResources()。getConfiguration() .orientation == Configuration.ORIENTATION_PORTRAIT){
cu = CameraUpdateFactory.newLatLngBounds(bounds,ViewUtils.dpAPixeles(MARKERS_DP_OFFSET,contexto));
} else {
//使用其他方法获取相机更新,例如CameraUpdateFactory.newLatLng(latLng);
}


I am developing an android app. In this app I am showing a MapView in a fragment. If my device is rotated and I try to move the camera using one of the following methods:

mapa.animateCamera(cameraUpdate);

or

mapa.moveCamera(cameraUpdate);

Then my app crashes and shows the following error:

java.lang.IllegalStateException: View size is too small after padding
 at com.google.a.a.ab.b(Unknown Source)
 at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source)
 at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source)
 at com.google.maps.api.android.lib6.d.dw.a(Unknown Source)
 at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source)
 at com.google.maps.api.android.lib6.d.et.a(Unknown Source)
 at com.google.android.gms.maps.internal.j.onTransact(SourceFile:83)
 at android.os.Binder.transact(Binder.java:380)
 at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.moveCamera(Unknown Source)
 at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source)
 at com.accionplus.dbdplus.utils.MapUtils.centrarCamara(MapUtils.java:88)
 at com.accionplus.dbdplus.utils.MapUtils.centrarCamara(MapUtils.java:52)
 at com.accionplus.dbdplus.controladores.pdv.RutaProgramadaFragment$4.onMyLocationButtonClick(RutaProgramadaFragment.java:274)
 at com.google.android.gms.maps.GoogleMap$3.onMyLocationButtonClick(Unknown Source)
 at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
 at android.os.Binder.transact(Binder.java:380)
 at com.google.android.gms.maps.internal.be.a(SourceFile:81)
 at com.google.maps.api.android.lib6.d.ak.onClick(Unknown Source)
 at android.view.View.performClick(View.java:5156)
 at android.view.View$PerformClick.run(View.java:20755)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:145)
 at android.app.ActivityThread.main(ActivityThread.java:5832)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

I have googled it but with no success. I just have found that it was a reported issue for google but I do not know if there is any workaround to "solve" this issue. Any one can help me?

EDIT: I am adding more code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.accionplus.dbdplus.controladores.pdv.RutaProgramadaActivity">

    <LinearLayout
        android:id="@+id/card_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#15000000"
        android:orientation="vertical"/>

    <LinearLayout
        android:id="@+id/filtro_ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_container"
        android:layout_margin="10dp"
        android:gravity="left|top"
        android:orientation="horizontal"
        android:weightSum="1">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:gravity="left|top"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:orientation="horizontal"
                android:weightSum="1">

                <ImageButton
                    android:id="@+id/seleccionar_fecha_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="35dp"
                    android:background="@drawable/button"
                    android:src="@drawable/ic_date"
                    android:text="@string/buscar"
                    android:textColor="@color/color_text_primary"/>

                <TextView
                    android:id="@+id/fecha_consulta_tv"
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="5dp"
                    android:background="@drawable/rectangle"
                    android:gravity="center"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:background="@drawable/rectangle">

                <Spinner
                    android:id="@+id/tipo_ruta_sp"
                    android:layout_width="fill_parent"
                    android:layout_height="35dp"
                    android:layout_gravity="center"
                    />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

    <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/filtro_ll"/>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:foregroundGravity="center|top"
        android:visibility="gone"/>

</RelativeLayout>

Here's when MyLocationButton is clicked:

mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {
                MapUtils.centrarCamara(getActivity(), mMap, mPuntosFinales);
                return true;
            }
        });

Here's MapUtils.centrarCamara() method:

public static void centrarCamara(Context contexto, GoogleMap mapa, LatLng... posiciones) {
        centrarCamara(contexto, mapa, Boolean.TRUE, posiciones);
    }

    public static void centrarCamara(Context contexto, GoogleMap mapa, boolean animado,
                                     LatLng... posiciones) {
        if (mapa == null) {
            return;
        }

        LatLngBounds.Builder builder = new LatLngBounds.Builder();
        for (LatLng posicion : posiciones) {
            builder.include(posicion);
        }

        LatLngBounds bounds = builder.build();

        CameraUpdate cu = CameraUpdateFactory
                .newLatLngBounds(bounds, ViewUtils.dpAPixeles(MARKERS_DP_OFFSET, contexto));

        if (animado) {
            mapa.animateCamera(cu);
        } else {
            mapa.moveCamera(cu);
        }
    }

ViewUtils.dpAPixeles:

public static int dpAPixeles(int dp, Context contexto) {
    Resources r = contexto.getResources();
    int px = (int) TypedValue
            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());

    return px;
}

The exception occurrs when mapa.moveCamera() or mapa.animateCamera() are called;

解决方案

From the documentation

Do not change the camera with this camera update until the map has undergone layout (in order for this method to correctly determine the appropriate bounding box and zoom level, the map must have a size). Otherwise an IllegalStateException will be thrown. It is NOT sufficient for the map to be available (i.e. getMap() returns a non-null object); the view containing the map must have also undergone layout such that its dimensions have been determined. If you cannot be sure that this has occured, use newLatLngBounds(LatLngBounds, int, int, int) instead and provide the dimensions of the map manually.

Thus, your problem may be that the map doesn't have a size when you are calling mapa.animateCamera(cameraUpdate) or mapa.moveCamera(cameraUpdate).

Anyway, as you say that the exception is being thrown when you rotate your device, I would say that the real issue is that the padding that you are using when calling newLatLngBounds (ViewUtils.dpAPixeles(MARKERS_DP_OFFSET, contexto)) is causing problems (maybe the padding is bigger than the View's height / 2?). There is a registered issue (4773) related to Google Maps that may help you solve your problem. As stated there, one possible workaround is to set the cameraUpdate depending on the orientation:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    cu = CameraUpdateFactory.newLatLngBounds(bounds, ViewUtils.dpAPixeles(MARKERS_DP_OFFSET, contexto));
} else {
    // Use another method to get your camera update, for example CameraUpdateFactory.newLatLng(latLng);
}

这篇关于IllegalStateException:填充后视图大小太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆