Reputation: 138
I have a listbox where I choose the elements I want to download by a checkbox, and a FloatingActionButton to confirm. The problem is that on my phone(Android 5.0.2) I get a "Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference" On my tablet (Nexus7 Android 6.0) everything works!
FloatingActionButtonDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < ListActivityListView.getCount(); j++) {
v = ListActivityListView.getChildAt(j);
//Here im having troubles!
ck = (CheckBox) v.findViewById(R.id.ListActivityCheckBox);
if (ck.isChecked()) {
/*
Stuff to download ++
*/
}
}
/*
Here I have the code to download my stuff
*/
}//End of onClick
});//End of FloatingActionButtonDone.setOnClickListener
Where: - ck is a CheckBox ck, declared in my class variables. - ListActivityListView is the ListView inside my activity.
I repeat same code works on my tablet, but doesn't on my phone. So i have literally no idea. Since my project is composed by 12 classes, I tried to post here the relevant code I'm having trouble with.
Adapter:
public class ListActivityAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Characters> characters;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ListActivityAdapter(Activity activity, List<Characters> personaggi) {
this.activity = activity;
this.characters = personaggi;
}
@Override
public int getCount() {
return characters.size();
}
@Override
public Object getItem(int location) {
return characters.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_drawer_item, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.ListActivityCheckBox);
NetworkImageView Immagine = (NetworkImageView) convertView.findViewById(R.id.ListActivityImage);
TextView Nome = (TextView) convertView.findViewById(R.id.ListActivityTitle);
TextView Peso = (TextView) convertView.findViewById(R.id.ListActivitySize);
Characters p = characters.get(position);
Immagine.setImageUrl(p.getImage(), imageLoader);
Nome.setText(p.getName());
Peso.setText(p.getSize());
return convertView;
}
}
Upvotes: 0
Views: 175
Reputation: 1986
which one is show the error? the checkbox or floating button? in this answer i assume it is the checkbox.
you need to use holder on your adapter, because the item in listview will be recycled and reused. add something like this on your adapter :
Public class Holder{
CheckBox yourCheckBox;
TextView yourTextview;
}
since we need to reuse it, you need to setTag to your holder if your convertView returns null (of course you need new xml which contains all item in single cell in your listview).
do something like this on your getView function :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Holder holder;
LayoutInflater inflater = (LayoutInflater) yourContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(R.layout.item_cell_listview, parent,false);
holder = new Holder();
holder.yourCheckBox = (ImageView)convertView.findViewById(R.id.yourCheckBox);
holder.yourListView = (ImageView)convertView.findViewById(R.id.yourListView);
convertView.setTag(holder);
}else{
holder = (Holder)convertView.getTag();
}
/*after get the holder and convert view, do your on click listener here*/
holder.yourCheckBox.setOnCheckChangedListener(/*blablabla*/){
/*blabla*/
}
}
Upvotes: 1
Reputation: 1470
This is not the correct way to use ListView, at least because the item views are discarded or recycled when they become invisible (say, when the list scrolled). The correct solution would be to save the checkbox state when it is modified, and query the list adapter, not the ListView.
So, in your list adapter:
public View getView (int position, View convertView, ViewGroup parent) {
View view = View.inflate(parent.getContext(),R.id.?,null);
CheckBox cb = view.findViewById(R.id.ListActivityCheckBox);
cb.setOnCheckedChangeListener (new CompoundButton.OnCheckedChangeListener listener(){
public void onCheckedChanged (CompoundButton buttonView, boolean isChecked){
// save isChecked here
}
});
...
}
And also provide a method in your adapter to query checked status of an item.
Upvotes: 0