GridView показывает NullPointerException, когда я его установил - PullRequest
0 голосов
/ 07 ноября 2019

Я выделил исключение NullPointerException как GridView, по какой-то причине внутри моего фрагмента он не может получить GridView из XML, или GridView по какой-то причине пуст, в базе данных определенно есть данные.

Выполнение оператора if, проверяющего GridView, проходит через null, но затем я получаю другие ошибки, такие как слишком большой BLOB-объект, с которым я могу попытаться справиться позже.

Фрагмент с помощью GridView

public class CollectionFragment extends Fragment {

    GridView gridView;
    ArrayList<Collectable> list;
    CollectionListAdapter adapter = null;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        setHasOptionsMenu (false);
        View view = inflater.inflate(R.layout.fragment_scanner, null);

        gridView = (GridView) view.findViewById(R.id.gridView);

        list = new ArrayList<>();

        //if (gridView != null) {

            adapter = new CollectionListAdapter(getActivity().getApplicationContext(), R.layout.images_carousel, list);
            gridView.setAdapter(adapter);

        //}


        Cursor cursor = ScannerPopup.sqLiteHelper.getData("SELECT * FROM TABLE_COLLECTION");
        list.clear();

        while(cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            String box = cursor.getString(2);
            String prod = cursor.getString(3);
            String date = cursor.getString(4);
            String store = cursor.getString(5);
            String country = cursor.getString(6);
            byte[] image = cursor.getBlob(7);

            list.add(new Collectable(name, box, prod, date, store, country, image, id));
        }

        adapter.notifyDataSetChanged();

        return view;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:paddingBottom="20dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="20dp"    >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:fontFamily="@font/regular"
        android:text="Collection"
        android:textColor="@color/darkgrey"
        android:textSize="50sp" />

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:columnWidth="160dp"
        android:gravity="center"
        android:numColumns="auto_fit" />


</RelativeLayout>

Адаптер списка сбора

public class CollectionListAdapter extends BaseAdapter {

    private Context context;
    private int layout;
    private ArrayList<Collectable> collectablesList;

    public CollectionListAdapter(Context context, int layout, ArrayList<Collectable> collectablesList) {
        this.context = context;
        this.layout = layout;
        this.collectablesList = collectablesList;
    }

    @Override
    public int getCount() {
        return collectablesList.size();
    }

    @Override
    public Object getItem(int position) {
        return collectablesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {

        ImageView imageView;
        TextView txtName, txtBox, txtProd, txtDate, txtStore, txtCountry;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        View row = view;
        ViewHolder holder = new ViewHolder();

        if (row != null) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(layout, null);

            holder.txtName = (TextView) row.findViewById(R.id.txtName);
            holder.txtBox = (TextView) row.findViewById(R.id.txtBox);
            holder.txtProd = (TextView) row.findViewById(R.id.txtProd);
            holder.txtDate = (TextView) row.findViewById(R.id.txtDate);
            holder.txtStore = (TextView) row.findViewById(R.id.txtStore);
            holder.txtCountry = (TextView) row.findViewById(R.id.txtCountry);
            holder.imageView = (ImageView) row.findViewById(R.id.imgCollectable);

            row.setTag(holder);

        }
        else {
            holder = (ViewHolder) row.getTag();

        }

        Collectable collectable = collectablesList.get(position);

        holder.txtName.setText(collectable.getName());
        holder.txtBox.setText(collectable.getBox());
        holder.txtProd.setText(collectable.getProd());
        holder.txtDate.setText(collectable.getDate());
        holder.txtStore.setText(collectable.getStore());
        holder.txtCountry.setText(collectable.getCountry());

        byte[] collectableImage = collectable.getImage();
        Bitmap bitmap = BitmapFactory.decodeByteArray(collectableImage, 0, collectableImage.length);

        holder.imageView.setImageBitmap(bitmap);

        return row;
    }
}

Сбор

public class Collectable {
    private int id;
    private String name;
    private String box;
    private String prod;
    private String date;
    private String store;
    private String country;
    byte[] image;

    public Collectable(String name, String box, String prod, String date, String store, String country, byte[] image, int id) {
        this.name = name;
        this.box = box;
        this.prod = prod;
        this.date = date;
        this.store = store;
        this.country = country;
        this.image = image;
        this.id = id;

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBox() {
        return box;
    }

    public void setBox(String box) {
        this.box = box;
    }

    public String getProd() {
        return prod;
    }

    public void setProd(String prod) {
        this.prod = prod;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getStore() {
        return store;
    }

    public void setStore(String store) {
        this.store = store;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }
}

Сбор XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imgCollectable"
        android:layout_width="124dp"
        android:layout_height="124dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="28dp"
        android:layout_marginTop="30dp"
        app:srcCompat="@mipmap/ic_launcherlogo" />

    <TextView
        android:id="@+id/txtName"
        android:layout_width="117dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="20dp"
        android:textAlignment="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/txtProd"
        android:layout_width="119dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtBox"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="13dp"
        android:textAlignment="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/txtBox"
        android:layout_width="119dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtName"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="19dp"
        android:textAlignment="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/txtDate"
        android:layout_width="119dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtProd"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="18dp"
        android:textAlignment="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/txtStore"
        android:layout_width="119dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtDate"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="14dp"
        android:textAlignment="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/txtCountry"
        android:layout_width="119dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtStore"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="16dp"
        android:textAlignment="center"
        android:text="TextView" />

</RelativeLayout>

Это основное сообщение об ошибке:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at com.example.vinylchainv2.CollectionFragment.onCreateView(CollectionFragment.java:36)
...