【发布时间】:2021-08-24 05:30:22
【问题描述】:
当图像加载到回收器视图https://stackoverflow.com/a/67860281/15537898 时,我正在尝试将高度和宽度从固定更改为匹配父级或包装内容,这是我正在尝试实现的答案,但在它给了我这个错误之后
尝试调用虚方法'int android.graphics.Bitmap.getHeight()' 在空对象引用上
Fragment_Search.java
ShapeableImageView siv;
Bitmap bm;
int height = bm.getHeight();
int width = bm.getWidth();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
postRecyclerView = view.findViewById(R.id.postRecyclerView);
siv = view.findViewById(R.id.imageView);
ViewGroup.LayoutParams params = siv.getLayoutParams();
params.height = height;
params.width = width;
postRecyclerView.setLayoutManager(
new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
);
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
getData();
mUploads = new ArrayList<>();
postsAdapter = new PostAdapter_Search(getContext(), mUploads);
postRecyclerView.setAdapter(postsAdapter);
return view;
}
【问题讨论】:
-
这个错误发生在哪一行?您是否已将 bm 变量初始化为某个位图,或者您是否只是声明了它?你需要提供更多的细节哥们!
-
好的兄弟我已经给出了整个错误,请检查问题
-
当我点击错误时,它会将我带到第 44 行,这是我实现这行代码的屏幕方向行 requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);你可以在问题中看到
-
这不是你应该做的。你的
bm甚至没有初始化。首先,如何将图像设置为您的ShapeImageView?以这种方式获取图像的宽度/高度。并且在重新加载图像之前不要将布局参数设置回扭曲内容/匹配父级。它甚至可能不再需要了。
标签: android bitmap layoutparams android-layoutparams