samedi 26 mai 2018

How to handle Checkbox click events

I am trying to handle check events of Checkbox, Checkbox is in custom listview, whenever I'm clicking on Checkbox my app is crashing with null pointer exception. I want to set checkbox checked by default but whenever I'm changing the state of checkbox my app is crashing. Here is the getView method

enter code here
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    // TODO Auto-generated method stub
    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    convertView = inflater.inflate(R.layout.row, parent, false); 
    viewholder = new ViewHolder();
    viewholder.checkbox = (CheckBox) convertView.findViewById(R.id.cb);
    viewholder.texview = (TextView) convertView.findViewById(R.id.tvv);
    viewholder.texview.setText(modelItems.get(position));
    viewholder.checkbox.setChecked(true);
    viewholder.checkbox.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View p1)
            {
                // TODO: Implement this method
                Toast.makeText(context,"checkbox item"+modelItems.get(position),Toast.LENGTH_SHORT).show();
                if (((CheckBox) p1).isChecked())
                {
                    checkBoxState[position] = true;
                    }

                else
                {
                    checkBoxState[position] = false;
                }
            }
        });
    return convertView;
}

Logcat

enter code here
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           FATAL EXCEPTION: main
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           Process: com.sk.scdoenloader, PID: 7489
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           java.lang.NullPointerException
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at com.sk.scdoenloader.CustomAdapter$100000000.onClick(CustomAdapter.java:63)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at android.view.View.performClick(View.java:4463)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at android.widget.CompoundButton.performClick(CompoundButton.java:100)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at android.view.View$PerformClick.run(View.java:18789)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at android.os.Handler.handleCallback(Handler.java:808)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at android.os.Handler.dispatchMessage(Handler.java:103)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at android.os.Looper.loop(Looper.java:193)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at android.app.ActivityThread.main(ActivityThread.java:5299)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at java.lang.reflect.Method.invokeNative(Native Method)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at java.lang.reflect.Method.invoke(Method.java:515)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
05-26 21:55:09.937 7489 7489 E     AndroidRuntime com.sk.scdoenloader           at dalvik.system.NativeStart.main(Native Method)




Aucun commentaire:

Enregistrer un commentaire