我希望这里的某个人可能知道导致这种行为的原因:
在我的整个应用程序中,在看似随机的地方和随机条件下,我都在观察这个奇怪的 UI 问题。图像有时被加载为黑色(具有正确的边界)或不正确的图像源(再次,具有正确的边界)。这会影响 ImageViews
并已生效 android:background
带有对颜色资源的引用的标签。
我的应用程序依赖于 6 个库项目,它通过服务运行 native 代码,应用程序中的 Activity 使用 GlSurfaceViews(尽管并非所有显示问题的 Activity 都包含 OpenGL 组件)。我认为问题可能来自这些地方中的任何一个或通过使用大量内存来组合它们。
您可以在以下屏幕截图中看到此行为:
正如您还可以看到它旁边的放大镜图像在每个图像中都显示得很好。这些丢失/不正确的图像和背景的问题似乎在整个应用程序生命周期中随机发生,我一直无法找到重现它的方法。
这些图像的布局没有什么特别之处,在渲染生命周期中我没有做任何有趣的事情(我没有覆盖
onDraw()
或 onMeasure()
等)。这些图像的来源不是动态设置的,而是通过 XML 设置的。正如您从上面的示例中看到的那样,这不是构建问题,因为它发生在应用生命周期之间而不是安装之间。它也发生在不同的设备上,三星 8.9、Acer Iconia Tab、Motarola XOOM、
在我看来,引用表有某种错误,它可能是由我的 native 代码插入的吗?或者它是我在应用程序的某些阶段使用太多内存的影响?
以下是上述示例的 XML 源代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/browseProgressWrapper"
android:layout_width="match_parent"
android:layout_height="@dimen/actionbar_compat_height"
android:orientation="horizontal">
<RelativeLayout android:layout_width="@dimen/search_bar_width"
android:layout_height="match_parent">
<EditText android:id="@+id/browseFilter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_marginLeft="5dp"
android:imeOptions="actionSearch"
android:background="@drawable/edit_text_blue"
android:maxLength="30"/>
<ImageView android:id="@+id/clearSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_input_delete"
android:layout_marginRight="5dp"/>
</RelativeLayout>
<ImageView android:id="@+id/browseFilterButton"
android:src="@drawable/ic_menu_search"
android:scaleType="center"
android:layout_width="@dimen/actionbar_compat_height"
android:layout_height="@dimen/actionbar_compat_height"
android:layout_gravity="center_vertical"
android:minWidth="@dimen/actionbar_compat_height"/>
</LinearLayout>
我碰巧得到了另一个这样的事件的代码/布局的更完整描述:我有一个“设置”
Activity
在保存新设置详细信息后重新启动我的应用程序。它通过停止 Service
来做到这一点。 , 调用新的 Activity
(飞溅 Activity )和完成本身: mConfiguration.save();
mConfiguration = new Configuration(Configuration.getInstance());
getActivity().stopService(new Intent(getActivity(), NativeService.class));
getActivity().finish();
startActivity(new Intent(getActivity(), SplashActivity.class));
大多数时候(在大多数设备上)这工作正常,Splash Activity 包含正确加载的图像。有时,尽管在某些设备上,Splash Activity 加载了不正确的资源(我的测试人员称之为“颠倒的耐克刻度”)或只是一个空白框(如下所示)。有谁知道为什么?这是 Splash 页面的布局,您可以看到它非常简单,毫不奇怪:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/ContentBackgroundColor"
android:orientation="vertical" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/manager_android_400" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
理论测试和揭秘:我推测这可能是处理器/内存问题,在启动画面退出并移动到下一个 Activity 之前布局没有被完全绘制,所以我输入了这段代码:
image = (ImageView) findViewById(R.id.image);
image.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
image.getViewTreeObserver().removeGlobalOnLayoutListener(this);
moveToStartScreen.start();
}
});
希望上面的代码可以确保在移动到开始页面之前肯定加载了图像,但似乎没有明显的效果。另一种理论
我还想知道这是否可能是由 R.id/R.colour/R.drawable 资源引起的,在程序执行中如何被破坏?有谁知道为什么会发生这种情况。
我的 native 代码是否会在一些 Android 未正确分配的内存地址上猖獗?
以前有没有人注意到这一点 - 或者可能知道为什么会发生这种行为?
最佳答案
Graeme,我遇到了几乎相同的问题,发现是报告的 bug安卓平台的。我认为它在 3.0 版本中得到了纠正。您正在使用哪个 API 进行编译?尝试使用最后一个可用的 api 进行构建,并确保使用 JDK 1.6 进行编译
如果您的问题与此错误有关,这应该可以解决问题。
关于android - 在整个应用生命周期中随机丢失或不正确的图像和背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12562896/