vendredi 12 janvier 2018

Gridview with checkbox returns NullPointerException

I'm populating images from mysql database to the gridView from which the user selects images matching symptoms using checkbox. Everything works fine when the images are less than 10 but the problem comes when the gridview has more than 10 images, when the user presses the continue button It gives a null-pointer reference in Logcat

Gridview with image from database

 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.findViewById(int)' on a null object reference
                                                   at com.banmart.http://www.intergratedpestmanagement.ImageTest.CustomAdapterTwo$3.onClick(CustomAdapterTwo.java:120)

This is my CustomAdapterTwo.java

images = new ArrayList<>();

        mAdapter = new ImageAdapter(this, images);
        gridView.setAdapter(mAdapter);
proceed.setOnClickListener(new View.OnClickListener() {
            String outputStrArr;
            @Override
            public void onClick(View v) {


                int count = gridView.getAdapter().getCount();

                ArrayList<String> selectedItems = new ArrayList<String>();
                for(int i = 0; i < count; i++) {

                    LinearLayout linearLayout = (LinearLayout) gridView.getChildAt(i);
                    checkbox = (CheckBox) linearLayout.findViewById(R.id.checkBox1);

                    if(checkbox.isChecked()){
                        outputStrArr =checkbox.getTag().toString();
                        selectedItems.add(outputStrArr);

                        String [] strArr = new String[selectedItems.size()];
                        for (int j= 0; j < selectedItems.size(); j++) {
                            strArr[j] = selectedItems.get(j);
                        }
                        Intent intent = new Intent(getApplicationContext(),
                                ResultActivity.class);

                        // Create a bundle object
                        Bundle b = new Bundle();
                        b.putStringArray("selectedItems", strArr);

                        // Add the bundle to the intent.
                        intent.putExtras(b);

                        // start the ResultActivity
                        startActivity(intent);

                        finish();
                        Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());
                        //  Toast.makeText(CustomAdapterTwo.this,checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();
//selectedItems.add(checkbox.getTag());
                    }
                }

            }

        });

ImageAdapter

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.gridtwo, null);
            viewHolder = new ViewHolder();

            viewHolder.textView = (TextView) convertView.findViewById(R.id.textView1);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
            viewHolder.Chkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);
            viewHolder.Chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();
                    images.get(getPosition).setSelected(buttonView.isChecked());
                }
            });
            convertView.setTag(viewHolder);
            convertView.setTag(R.id.textView1, viewHolder.textView);
            convertView.setTag(R.id.imageView1, viewHolder.imageView);
            convertView.setTag(R.id.checkBox1, viewHolder.Chkbox);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ImagesLib image = images.get(position);
       viewHolder.textView.setText(image.getAltText());


        Glide.with(mContext).load(image.getImageUrl())
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewHolder.imageView);

        viewHolder.Chkbox.setTag(image.getId());
viewHolder.Chkbox.setTag(position);
viewHolder.Chkbox.setChecked(images.get(position).isSelected());
        return convertView;
    }

grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginRight="10px"
         >
    </ImageView>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true" />


</LinearLayout>

grid_gallery.xml

<TableLayout xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:textColor="#050"
            android:textStyle="bold"
            android:layout_marginLeft="5dp"
            android:text="Check the symptoms on the Banana Plant "
            android:layout_span="1"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </TableRow>


    <View
        android:layout_height="1dip"
        android:background="#CCCCCC" />



    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.1">

        <GridView
            android:id="@+id/gridView1"
            android:layout_marginLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="auto_fit" >
        </GridView>

    </LinearLayout>



    <View
        android:layout_height="1dip"
        android:background="#CCCCCC" />


    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:weightSum="3">

        <Button
            android:id="@+id/btnCheckAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Check All" />
        <Button
            android:id="@+id/btnClearAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Clear All" />
        <Button
            android:id="@+id/btnGetItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#070"
            android:textColor="#fff"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="Continue >>" />

    </LinearLayout>

</TableLayout>




Aucun commentaire:

Enregistrer un commentaire