由xml布局引起的ANR - 在极少数设备上 - 维度异常

时间:2015-03-31 08:05:44

标签: android layout dimensions

我有一个名为activity_login.xml的布局文件。这是代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" 
android:fillViewport="true" 
android:layout_height="match_parent"
android:id="@+id/scroll_container"
android:foregroundGravity="fill_horizontal|center_vertical|clip_horizontal|fill_vertical|clip_vertical|center_horizontal"
android:layoutMode="clipBounds"
android:longClickable="false"
android:background="@drawable/background_portret">

<LinearLayout 
    android:layout_width="match_parent"
    android:fillViewport="true" 
    android:layout_height="match_parent"
    android:scaleType="center"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingRight="12dp"
    android:paddingTop="200sp" 
    tools:context=".LoginActivity"
    android:baselineAligned="true"
    android:weightSum="1"
    android:id="@+id/ui_container"
    android:layoutMode="clipBounds"
    android:orientation="vertical"
    android:paddingLeft="12dp"
    android:gravity="center">

    <!-- Login progress -->

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:id="@+id/login_progress3"
        android:layout_weight="0.25"
        android:visibility="gone" />

    <TextView
        android:layout_width="299dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:layout_weight="0.50"
        android:visibility="gone" />

    <ScrollView android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="visible">

        <LinearLayout android:id="@+id/email_login_form" android:layout_width="match_parent"
            android:layout_height="wrap_content" android:orientation="vertical"
            android:weightSum="1"
            android:layout_weight="0"
            android:gravity="fill_horizontal|center_vertical|fill_vertical|center_horizontal"
            android:layout_gravity="center">



            <EditText android:id="@+id/username" android:layout_width="match_parent"
                android:layout_height="40dp" android:hint="@string/prompt_username"
                android:maxLength="32"
                android:inputType="textFilter|textNoSuggestions"
                android:background="@drawable/chenar"
                android:drawableLeft="@drawable/icon_user"
                android:tileMode="repeat"
                android:maxLines="1" android:singleLine="true"
                android:textColor="#eeeff4"
                android:textStyle="normal"
                android:paddingLeft="3dp"
                android:paddingRight="10dp"
                android:layout_marginBottom="5dp"
                android:autoText="false"
                android:textSize="18sp"
                android:textColorHint="#eeeff4" />

            <EditText android:id="@+id/password" android:layout_width="match_parent"
                android:layout_height="40dp" android:hint="@string/prompt_password"
                android:imeActionId="@+id/login"
                android:maxLength="32"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified"
                android:singleLine="true"
                android:inputType="textPassword"
                android:drawableLeft="@drawable/icon_password"
                android:background="@drawable/chenar"
                android:textStyle="normal"
                android:layout_marginBottom="5dp"
                android:textColor="#eeeff4"
                android:textSize="18sp"
                android:paddingLeft="3dp"
                android:paddingRight="10dp"
                android:textColorHint="#eeeff4" />

            <Button android:id="@+id/email_sign_in_button2" style="?android:textAppearanceSmall"
                android:layout_width="350dp" android:layout_height="wrap_content"
                android:layout_marginTop="16dp" android:text="@string/action_sign_in"
                android:layout_gravity="center_horizontal"
                android:textStyle="bold|italic"
                android:visibility="gone" />
            <RelativeLayout android:layout_width="match_parent"
                android:id="@+id/relativeLayout1" android:layout_height="wrap_content">
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_marginLeft="3dp"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:text="@string/remCredentials"
                android:id="@+id/checkRemPass"
                android:button="@drawable/custom_checkbox_design"
                android:drawablePadding="12dp"
                android:checked="false"
                android:paddingLeft="1dp"
                android:singleLine="true" />

            <Button
                style="?android:attr/buttonStyleSmall"

                android:layout_width="102dp"
                android:layout_height="30dp"

                android:id="@+id/register"
                android:background="@drawable/custom_register_button"
                android:textAlignment="center"
                android:capitalize="none"
                android:layout_alignParentStart="false"
                android:layout_alignParentBottom="false"
                android:layout_alignParentTop="true"
                android:layout_alignWithParentIfMissing="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="false"
                android:textColor="#eeeff4"
                android:gravity="left|center_vertical"
                android:layout_marginRight="1dp"
                android:clickable="true"
                android:enabled="true"
                android:visibility="visible"
                android:layout_marginTop="1dp" />
            </RelativeLayout>
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp"
                android:text="@string/autologin"
                android:id="@+id/checkAutoLogin"
                android:button="@drawable/custom_checkbox_design"
                android:checked="false"
                android:paddingLeft="1dp"
                android:singleLine="true"
                android:layout_marginTop="3dp" />

            <Button
                android:layout_width="150dp"
                android:layout_height="35dp"
                android:background="@drawable/custom_play_button"
                android:text="@string/action_sign_in"
                android:id="@+id/email_sign_in_button"
                android:textColor="#fff4fffd"
                android:capitalize="none"
                android:layout_marginTop="20dp"

                android:nestedScrollingEnabled="false"
                android:clickable="false"
                android:singleLine="true"
                android:textStyle="normal"
                android:layout_gravity="top|bottom|center_horizontal"
                android:layout_marginBottom="20dp"
                android:backgroundTintMode="src_over"
                android:ellipsize="none"
                android:maxLines="1"
                android:textAlignment="center"
                android:visibility="visible"
                android:typeface="normal"
                android:layout_weight="0.45" />

        </LinearLayout>
    </ScrollView>

    <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" android:visibility="gone"
        android:focusable="false"
        android:contentDescription="Signing In..."
        android:focusableInTouchMode="false"
        android:layout_gravity="center" />


</LinearLayout>

现在问题是上述布局在大多数设备上运行良好。但是在某些设备上(2000年中有11个 - 谷歌播放报道 - 到目前为止 - 以上布局导致ANR异常无法转换为维度:类型= 0x10)

我尝试了一点清理,摆脱了TextViews,但用户仍然抱怨ANR。我也无法在我测试的设备上复制这个问题。

我从谷歌的游戏开发者控制台获得的原始日志:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.pack/com.test.pack.LoginActivity}: android.view.InflateException: Binary XML file line #164: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
    at android.app.ActivityThread.access$900(ActivityThread.java:148)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5457)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #164: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    at android.app.Activity.setContentView(Activity.java:1915)
    at com.test.pack.LoginActivity.onCreate(Unknown Source)
    at android.app.Activity.performCreate(Activity.java:5234)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
    ... 11 more
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
    ... 27 more
    Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x10
    at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:468)
    at android.widget.TextView.initAddtionalStyle(TextView.java:9212)
    at android.widget.TextView.<init>(TextView.java:1324)
    at android.widget.Button.<init>(Button.java:107)
    at android.widget.Button.<init>(Button.java:103)
    ... 30 more

非常感谢您的投入。

1 个答案:

答案 0 :(得分:0)

布局中的某些更正只是复制&amp;粘贴此

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" 
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scroll_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_portret"
        android:fillViewport="true"
        android:foregroundGravity="fill_horizontal|center_vertical|clip_horizontal|fill_vertical|clip_vertical|center_horizontal"
        android:layoutMode="clipBounds"
        android:longClickable="false" >

        <LinearLayout
            android:id="@+id/ui_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="true"
            android:fillViewport="true"
            android:gravity="center"
            android:layoutMode="clipBounds"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:paddingTop="200sp"
            android:scaleType="center"
            android:weightSum="1"
            tools:context=".LoginActivity" >

            <!-- Login progress -->

            <ProgressBar
                android:id="@+id/login_progress3"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="170dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:visibility="gone" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="299dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_margin="10dp"
                android:layout_weight="0.50"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:visibility="gone" />

            <ScrollView
                android:id="@+id/login_form"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:visibility="visible" >

                <LinearLayout
                    android:id="@+id/email_login_form"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="0"
                    android:gravity="fill_horizontal|center_vertical|fill_vertical|center_horizontal"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <EditText
                        android:id="@+id/username"
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_marginBottom="5dp"
                        android:autoText="false"
                        android:background="@drawable/chenar"
                        android:drawableLeft="@drawable/icon_user"
                        android:hint="@string/prompt_username"
                        android:inputType="textFilter|textNoSuggestions"
                        android:maxLength="32"
                        android:maxLines="1"
                        android:paddingLeft="3dp"
                        android:paddingRight="10dp"
                        android:singleLine="true"
                        android:textColor="#eeeff4"
                        android:textColorHint="#eeeff4"
                        android:textSize="18sp"
                        android:textStyle="normal"
                        android:tileMode="repeat" />

                    <EditText
                        android:id="@+id/password"
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_marginBottom="5dp"
                        android:background="@drawable/chenar"
                        android:drawableLeft="@drawable/icon_password"
                        android:hint="@string/prompt_password"
                        android:imeActionId="@+id/login"
                        android:imeActionLabel="@string/action_sign_in_short"
                        android:imeOptions="actionUnspecified"
                        android:inputType="textPassword"
                        android:maxLength="32"
                        android:paddingLeft="3dp"
                        android:paddingRight="10dp"
                        android:singleLine="true"
                        android:textColor="#eeeff4"
                        android:textColorHint="#eeeff4"
                        android:textSize="18sp"
                        android:textStyle="normal" />

                    <Button
                        android:id="@+id/email_sign_in_button2"
                        style="?android:textAppearanceSmall"
                        android:layout_width="350dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="16dp"
                        android:text="@string/action_sign_in"
                        android:textStyle="bold|italic"
                        android:visibility="gone" />

                    <RelativeLayout
                        android:id="@+id/relativeLayout1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <CheckBox
                            android:id="@+id/checkRemPass"
                            android:layout_width="wrap_content"
                            android:layout_height="30dp"
                            android:layout_marginLeft="3dp"
                            android:button="@drawable/custom_checkbox_design"
                            android:checked="false"
                            android:drawablePadding="12dp"
                            android:focusable="true"
                            android:focusableInTouchMode="true"
                            android:paddingLeft="1dp"
                            android:singleLine="true"
                            android:text="@string/remCredentials" />

                        <Button
                            android:id="@+id/register"
                            style="?android:attr/buttonStyleSmall"
                            android:layout_width="102dp"
                            android:layout_height="30dp"
                            android:layout_alignParentBottom="false"
                            android:layout_alignParentEnd="false"
                            android:layout_alignParentLeft="false"
                            android:layout_alignParentRight="true"
                            android:layout_alignParentTop="true"
                            android:layout_alignWithParentIfMissing="false"
                            android:layout_marginRight="1dp"
                            android:layout_marginTop="1dp"
                            android:background="@drawable/custom_register_button"
                            android:capitalize="none"
                            android:clickable="true"
                            android:enabled="true"
                            android:focusable="false"
                            android:focusableInTouchMode="false"
                            android:gravity="left|center_vertical"
                            android:textAlignment="center"
                            android:textColor="#eeeff4"
                            android:visibility="visible" />
                    </RelativeLayout>

                    <CheckBox
                        android:id="@+id/checkAutoLogin"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="3dp"
                        android:layout_marginTop="3dp"
                        android:button="@drawable/custom_checkbox_design"
                        android:checked="false"
                        android:paddingLeft="1dp"
                        android:singleLine="true"
                        android:text="@string/autologin" />

                    <Button
                        android:id="@+id/email_sign_in_button"
                        android:layout_width="150dp"
                        android:layout_height="35dp"
                        android:layout_gravity="top|bottom|center_horizontal"
                        android:layout_marginBottom="20dp"
                        android:layout_marginTop="20dp"
                        android:layout_weight="0.45"
                        android:background="@drawable/custom_play_button"
                        android:backgroundTintMode="src_over"
                        android:capitalize="none"
                        android:clickable="false"
                        android:ellipsize="none"
                        android:maxLines="1"
                        android:nestedScrollingEnabled="false"
                        android:singleLine="true"
                        android:text="@string/action_sign_in"
                        android:textAlignment="center"
                        android:textColor="#fff4fffd"
                        android:textStyle="normal"
                        android:typeface="normal"
                        android:visibility="visible" />
                </LinearLayout>
                </ScrollView>
                </LinearLayout>
            </ScrollView>

            <ProgressBar
                android:id="@+id/login_progress"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="8dp"
                android:contentDescription="Signing In..."
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:visibility="gone" />
       </LinearLayout>