Why my Context is null inside a Fragment created from FragmentStateAdapter?

I am trying to retrieve images from SQLite database (I saved the images as String path ) to an ImageView, using this method:

Load method

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Bitmap Load(String path, BitmapSize bitmapSize) {
BitmapFactory.Options options = SetBitmapOptions(bitmapSize);
Context ctx = getContext();
if (ctx == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a
context.");
} else {
Bitmap placeholder = decodeResource(ctx.getResources(),
R.drawable.placeholder, options);
try {
if (path.isEmpty()) {
return placeholder;
}
return decodeFile(path, options);
} catch (Exception e) {
Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
return placeholder;
}
}
}
</code>
<code>public Bitmap Load(String path, BitmapSize bitmapSize) { BitmapFactory.Options options = SetBitmapOptions(bitmapSize); Context ctx = getContext(); if (ctx == null) { throw new IllegalStateException("Fragment " + this + " not attached to a context."); } else { Bitmap placeholder = decodeResource(ctx.getResources(), R.drawable.placeholder, options); try { if (path.isEmpty()) { return placeholder; } return decodeFile(path, options); } catch (Exception e) { Log.e(TAG, "Exception: " + Log.getStackTraceString(e)); return placeholder; } } } </code>
public Bitmap Load(String path, BitmapSize bitmapSize) {

        BitmapFactory.Options options = SetBitmapOptions(bitmapSize);
        Context ctx = getContext();

        if (ctx == null) {
            throw new IllegalStateException("Fragment " + this + " not attached to a 
                   context.");
        } else {
            Bitmap placeholder = decodeResource(ctx.getResources(),
                    R.drawable.placeholder, options);
            try {
                  if (path.isEmpty()) {
                    return placeholder;
                }
                 return decodeFile(path, options);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + Log.getStackTraceString(e));
                return placeholder;
            }
        }
    }

This method is inside the Fragment FreezerUp, and I am calling this method from a ListViewAdapter implements ListAdapter class its method: getView

getView method

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FreezerUpFragment fuf = new FreezerUpFragment();
Product prd = mArrList.get(position);
if (convertView == null) {
LayoutInflater li = LayoutInflater.from(mContext);
convertView = li.inflate(R.layout.list_row, null);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
convertView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
TextView prdName = convertView.findViewById(R.id.productName);
ImageView image = convertView.findViewById(R.id.img);
TextView prdDesc = convertView.findViewById(R.id.productDesc);
prdName.setText(prd.getProduktName());
prdDesc.setText(prd.getProduktDesc());
String s = FreezerUpFragment.imgPath;
FreezerUpFragment.BitmapSize bs = FreezerUpFragment.BitmapSize.ListView;
fuf.Load(s, bs);
image.setImageBitmap(fuf.Load(s, bs));
}
return convertView;
</code>
<code>@SuppressLint("InflateParams") @Override public View getView(int position, View convertView, ViewGroup parent) { FreezerUpFragment fuf = new FreezerUpFragment(); Product prd = mArrList.get(position); if (convertView == null) { LayoutInflater li = LayoutInflater.from(mContext); convertView = li.inflate(R.layout.list_row, null); convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); convertView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { return true; } }); TextView prdName = convertView.findViewById(R.id.productName); ImageView image = convertView.findViewById(R.id.img); TextView prdDesc = convertView.findViewById(R.id.productDesc); prdName.setText(prd.getProduktName()); prdDesc.setText(prd.getProduktDesc()); String s = FreezerUpFragment.imgPath; FreezerUpFragment.BitmapSize bs = FreezerUpFragment.BitmapSize.ListView; fuf.Load(s, bs); image.setImageBitmap(fuf.Load(s, bs)); } return convertView; </code>
@SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        FreezerUpFragment fuf = new FreezerUpFragment();
        Product prd = mArrList.get(position);

        if (convertView == null) {
            LayoutInflater li = LayoutInflater.from(mContext);
            convertView = li.inflate(R.layout.list_row, null);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            convertView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return true;
                }
            });

            TextView prdName = convertView.findViewById(R.id.productName);
            ImageView image = convertView.findViewById(R.id.img);
            TextView prdDesc = convertView.findViewById(R.id.productDesc);

            prdName.setText(prd.getProduktName());
            prdDesc.setText(prd.getProduktDesc());

            String s = FreezerUpFragment.imgPath;
            FreezerUpFragment.BitmapSize bs = FreezerUpFragment.BitmapSize.ListView;
            
            fuf.Load(s, bs);
            image.setImageBitmap(fuf.Load(s, bs));
        }
        return convertView;

I am using a TabLayout with PagerAdapter extends FragmentStateAdapter class for 2 fragments FreezerUpFragment and FreezerDownFragment.

My app starts showing FreezerUpFragment in the first tab containing a ListView (product names, product description and product images).

After a debug I noticed that ctx: null in Load method.

Any help to suggest or point me to why the Context is null is very appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật