-1

I'm trying to change the height and the width from fixed to match parent or wrap content when images are loaded into the recycler view https://stackoverflow.com/a/67860281/15537898 here is the answer I'm trying to implement but after it gives me this error

Attempt to invoke virtual method 'int android.graphics.Bitmap.getHeight()' on a null object reference

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;
    }
  • Which line is this error occurring on ? Have you initialised the bm variable to some Bitmap or have you merely declared it ? You need to provide more details buddy ! – ADITYA RANADE Jun 07 '21 at 05:43
  • Ok brother I have given the entier Error, pls check the question –  Jun 07 '21 at 05:50
  • When i click the error it takes me to the 44th line which is the screen orientation line where I implemented this line of code requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); you can see in the question –  Jun 07 '21 at 05:53
  • That is not how you should do it. Your `bm` is not even initialised. First of all, how do you set an image to your `ShapeImageView`? Get the image's width/height that way. And don't set the layout params back to warp content/match parent before the image has been reloaded. It may not even necessery anymore. – Darkman Jun 07 '21 at 06:28

1 Answers1

1

The bitmap varaible (bm) is not initialized, even if you initialize it in onCreateView, you app will still crash because you are trying to get bitmap width and height before the bm variable is even initialized. You can do it this way

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_search, container, false);
    
    bm = initialize_bitmap_here
    width = bm.getWidth()
    height = bm.getHeght()


    return view;
}