0

When I Press in checkAll button it give NullpointerException occur in My custom ListView.

The problem is that, if screen has 6 or 7 item as per screen size than it has no problem checkAll working butif the items is more than the screen size then it gives NullPointerException.

I have put my code below:

// ///////////////////// checkAll / ClearAll        // ///////////////////////////////////////

    btnCheckAll.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            int count =lvHiddenApp.getAdapter().getCount();

            Log.i("NoOfApp", "" + count);

            if (state) {
                for (int i = 0; i < count; i++) {

                    RelativeLayout itemLayout = (RelativeLayout) lvHiddenApp
                            .getChildAt(i); // Find
                    CheckBox checkbox = (CheckBox) itemLayout
                            .findViewById(R.id.cbHideAppCheck);

                    checkbox.setChecked(true);
                    btnCheckAll.setText("UncheckAll");
                    state = false;
                }
            } else {
                for (int i2 = 0; i2 < count; i2++) {
                    RelativeLayout itemLayout = (RelativeLayout) lvHiddenApp
                            .getChildAt(i2); // Find
                    CheckBox checkbox = (CheckBox) itemLayout
                            .findViewById(R.id.cbHideAppCheck);

                    checkbox.setChecked(false);

                    btnCheckAll.setText("checkAll");
                    state = true;
                }
            }
        }

    });

    // ////////////////////////////////////////////////////////////////////////////////////////////////////

Logcat:

 E/AndroidRuntime(2420): FATAL EXCEPTION: main
 E/AndroidRuntime(2420): java.lang.NullPointerException
 E/AndroidRuntime(2420):at com.example.settings.HiddenAppList$1.onClick(HiddenAppList.java:92)
 E/AndroidRuntime(2420):at com.rey.material.widget.RippleManager.run(RippleManager.java:83)
 E/AndroidRuntime(2420):at com.rey.material.widget.RippleManager.onClick(RippleManager.java:77)
 E/AndroidRuntime(2420):at android.view.View.performClick(View.java:4240)
 E/AndroidRuntime(2420):at android.view.View$PerformClick.run(View.java:17721)
 E/AndroidRuntime(2420):at android.os.Handler.handleCallback(Handler.java:730)
 E/AndroidRuntime(2420):at android.os.Handler.dispatchMessage(Handler.java:92)
 E/AndroidRuntime(2420):at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(2420):at android.app.ActivityThread.main(ActivityThread.java:5103)
 E/AndroidRuntime(2420):at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(2420):at java.lang.reflect.Method.invoke(Method.java:525)
 E/AndroidRuntime(2420):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 E/AndroidRuntime(2420):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 03-09 12:17:48.083: E/AndroidRuntime(2420):at dalvik.system.NativeStart.main(Native Method)

xml file:

<?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" >

<ImageView
    android:id="@+id/ivHideAppIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@+id/img"
    android:src="@drawable/ic_launcher" />

<com.rey.material.widget.TextView
    android:id="@+id/tvHideAppText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/ivHideAppIcon"
    android:padding="4dp"
    android:text="@string/hidden_app" />

<CheckBox
    android:id="@+id/cbHideAppCheck"
    style="@style/CheckBoxDrawable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:checked="false" />

adapter file:

public class HideAppListviewAdapter extends BaseAdapter {                  

public HideAppListviewAdapter() {

    // TODO Auto-generated constructor stub
}

String i;
ArrayList<String> state = new ArrayList<String>();;
Context context;
private ArrayList<Category> items;
private LayoutInflater mInflater;

public class ViewHolder {
    public ImageView imageView;
    public TextView textTitle;
    public CheckBox cb;
    public Button btnitem;
}

public HideAppListviewAdapter(Context context, ArrayList<Category> cat) {

    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context = context;
    items = cat;
    Log.i("itemsizepela", "" + items.size());
}

public ArrayList<Category> getItems() {
    return items;
}

public void setItems(ArrayList<Category> items) {
    this.items = items;
}

@Override
public int getCount() {
    if (items != null) {
        return items.size();
    }
    return 0;
}

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

@Override
public Category getItem(int position) {
    if (items != null && position >= 0 && position < getCount()) {
        return items.get(position);
    }
    return null;
}

@Override
public long getItemId(int position) {
    if (items != null && position >= 0 && position < getCount()) {
        return items.get(position).id;
    }
    return 0;
}

public void setItemsList(ArrayList<Category> locations) {
    this.items = locations;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View view = convertView;
    final ViewHolder viewHolder;
    final Category gridItems = getItem(position);

    if (view == null) {

        view = mInflater.inflate(R.layout.hidden_app_list_item, parent,
                false);

        viewHolder = new ViewHolder();

        viewHolder.imageView = (ImageView) view
                .findViewById(R.id.ivHideAppIcon);
        viewHolder.textTitle = (TextView) view
                .findViewById(R.id.tvHideAppText);
        viewHolder.cb = (CheckBox) view.findViewById(R.id.cbHideAppCheck);

        view.setTag(viewHolder);
        CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbHideAppCheck);
        cbBuy.setOnCheckedChangeListener(myCheckChangList);
        cbBuy.setTag(position);
        cbBuy.setChecked(gridItems.box);

    } else {
        viewHolder = (ViewHolder) view.getTag();
    }

    setCatImage(position, viewHolder, gridItems.name, gridItems.icon);

    return view;
}

@SuppressLint("NewApi")
private void setCatImage(int pos, ViewHolder viewHolder, String catTitle,
        Drawable icon) {
    viewHolder.imageView.setImageDrawable(icon);
    viewHolder.textTitle.setText(catTitle);
}

ArrayList<Category> getBox() {
    ArrayList<Category> box = new ArrayList<Category>();
    for (Category p : items) {
        if (p.box)
            box.add(p);
    }
    return box;
}

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        getItem((Integer) buttonView.getTag()).box = isChecked;

    }
};
}
Mel
  • 5,837
  • 10
  • 37
  • 42

2 Answers2

0

You're getting a null return value from lvHiddenApp.getChildAt(i). ListViews only keep inflated views for on-screen items. When an item goes off screen, the view is reused for a new item, and simply filled with the new data (in your adapter).

You have to keep your item state (like selection) in your adapter, not the UI components.

Make sure that your adapter implementation properly initializes all the views' state, or I imagine you'll get some interesting problems (try checking a single item in a long list, then scroll down, and you'll see what I mean).

Snild Dolkow
  • 6,669
  • 3
  • 20
  • 32
-1

Did you try to click the button even before setting adapter to list view? Otherwise put either complete code or logcat logs.

Mahendra Chhimwal
  • 1,810
  • 5
  • 21
  • 33