首页 > 解决方案 > Android 应用程序因配置更改而崩溃

问题描述

我的 android 应用程序被迫以横向模式运行。我的设备从不改变它的方向,并且我在运行时没有更改任何配置(至少不是故意的),在运行期间没有人触摸设备,但是,我看到应用程序每 2-3 分钟就不断崩溃,配置更改在此之前触发:

2019-07-22 00:35:34.222 642-1695/? E/SurfaceFlinger: size mismatch (2960, 1440) > (1440, 2960)
2019-07-22 00:35:34.242 1135-1146/? I/ActivityManager: Config changes=20000480 {1.0 ?mcc?mnc [en_US] ldltr sw411dp w411dp h748dp 560dpi nrml long hdr widecg port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 0, 0) mAppBounds=Rect(0, 171 - 1440, 2792) mWindowingMode=fullscreen mActivityType=undefined} s.45}
2019-07-22 00:35:34.263 1135-1146/? I/ActivityManager: Override config changes=20000480 {1.0 ?mcc?mnc [en_US] ldltr sw411dp w411dp h748dp 560dpi nrml long hdr widecg port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 2621, 1440) mAppBounds=Rect(0, 171 - 1440, 2792) mWindowingMode=fullscreen mActivityType=undefined} s.45} for displayId=0
2019-07-22 00:35:34.316 1135-1281/? I/InputReader: Device reconfigured: id=2, name='sec_touchscreen', size 1440x2960, orientation 0, mode 1, display id 0
2019-07-22 00:35:34.471 642-642/? E/SurfaceFlinger: [com.breel.wallpapers18.soundviz.wallpaper.variations.SoundVizWallpaperV2#0] rejecting buffer: bufWidth=2960, bufHeight=1440, front.active.{w=1440, h=2960}
2019-07-22 00:35:34.666 1410-1410/? W/StatusBar/NavBarView: *** Invalid layout in navigation bar (sizeChanged this=1440x168 cur=1384x168)

我想我可能以某种方式搞砸了布局,但我不确定这是否是导致问题的原因,无论如何这里是我的 layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainLayout">

    <io.occo.vision.camera2.AutoFitTextureView
        android:id="@+id/tele_texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:visibility="visible" />

    <io.occo.vision.camera2.AutoFitTextureView
        android:id="@+id/wide_texture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="120dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:visibility="visible" />

    <io.occo.vision.camera2.OverlayView
        android:id="@+id/overlay_view_wide"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="120dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <io.occo.vision.camera2.OverlayView
        android:id="@+id/overlay_view_tele"
        android:layout_width="match_parent"
        android:layout_marginStart="120dp"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <io.occo.vision.camera2.OverlayView
        android:id="@+id/debug_overlay_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:alpha="1"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/unpin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:onClick="unpin"
        android:stateListAnimator="@null"
        android:text="unpin" />

    <TextView
        android:id="@+id/version"
        android:layout_width="200dp"
        android:layout_height="34dp"
        android:layout_alignParentStart="true"
        android:layout_marginStart="122dp"
        android:text="version: 0.0.2"
        android:textAlignment="textStart" />
</RelativeLayout>

这是我在启动时的活动配置

  @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        View decorView = getWindow().getDecorView();
        if (hasFocus) {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            );
        }
    }

清单.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity
            android:name="io.occo.vision.CameraActivity"
            android:lockTaskMode="if_whitelisted"
            android:exported="true"
            android:label="@string/app_name"
            android:noHistory="true"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.PROFILE_PROVISIONING_COMPLETE" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.firebase.ml.vision.DEPENDENCIES"
            android:value="face"
            tools:replace="android:value" />
    </application>

我的应用在 Pixel 3XL、Android 9 (Pi) 上运行,API 级别:28

标签: android

解决方案


我的 android 应用程序被迫以横向模式运行。我的设备从不改变它的方向,我在运行时没有改变任何配置(至少不是故意的),在运行过程中没有人触摸设备

A:manifest那是因为你已经要求布局在<activity 标签中强制以横向方向运行, android:screenOrientation="landscape".

问:在运行期间没有人触摸设备,然而,我看到应用程序每隔 2-3 分钟就一直崩溃一次,并且在此之前触发了配置更改:

A:如果你想将你的方向设置为横向,那么你必须基于它来设计页面。正如您在日志中看到的那样Config changes=20000480 {1.0 ?mcc?mnc [en_US] ldltr sw411dp w411dp h748dp 560dpi,这些只是屏幕的不同密度,因此您的布局正在崩溃。

请参阅https://developer.android.com/guide/practices/screens_support以获得关于设计布局的清晰和更好的理解。


推荐阅读