我的Firebase崩溃报告显示以下代码在极少数设备上出现故障。我无法重现此问题。是否有人能提出可能的问题?似乎问题是imageView.getWidth()或imageView.getHeight()返回零。
以下代码位于我的fragment中:
这是布局:
以下代码位于我的fragment中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_category_list, container, false);
final ImageView imageView = (ImageView) view.findViewById(R.id.categoryBackgrndImageView);
// Set an observer that is called after the imageView is laid out
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//now we can retrieve the width and height
int width = imageView.getWidth();
int height = imageView.getHeight();
// etc. Code here removed. It gets an asset and creates a bitmap from the
// asset, sizing the bitmap based on the imageView width and height
}
这是布局:
<FrameLayout 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:menu="menu_main,menu_categories">
<ImageView
android:id="@+id/categoryBackgrndImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<technology.grandma.margriver.AutoGridRecylerView
android:id="@+id/categoryRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="330dp"
android:scrollbars="vertical"
tools:listitem="@layout/list_item_category"/>
</FrameLayout>
我的互联网研究似乎证实了这段代码是正确的。你能看出有什么问题吗?