我正在点击图片View更改图片视图图标,但是当它在列表视图中返回空指针异常错误时。
这是我的代码。请帮我解决这个问题
public class SelectTagAdapter extends BaseAdapter implements Filterable {
Context context;
ArrayList<TagDataModel> arrayList;
LayoutInflater inflater=null;
FilterSearchListener filterSearchListener;
String selectedTag;
TagDatabaseHelper tagDatabaseHelper;
SelectTagActivity tagActivity;
String tagType;
static int AT_TAG_TYPE=1;
static int HASH_TAG_TYPE=0;
Holder holder=null;
View view;
public SelectTagAdapter(Context con,ArrayList arr,String tagType){
this.context=con;
this.arrayList=arr;
this.tagType = tagType;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tagActivity=(SelectTagActivity)context;
tagDatabaseHelper = new TagDatabaseHelper(context);
}
public void setFilterListener(FilterSearchListener filterSearchListener) {
this.filterSearchListener=filterSearchListener;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return arrayList.get(position).hashCode();
}
@Override
public int getItemViewType(int position) {
return arrayList.get(position).getViewType() ;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TagDataModel tag=new TagDataModel();
View rowView=null;
int viewType = getItemViewType(position);
if(rowView==null) {
holder =new Holder();
switch (viewType)
{
case 0://For # type Tags
rowView = inflater.inflate(R.layout.tag_item_hash_type, null);
view = rowView;
holder.checked = (ImageView)rowView.findViewById(R.id.checked);
holder.relativeLayout = (RelativeLayout)rowView.findViewById(R.id.tag_realtivelayout);
holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getTagStatus("", position, holder, HASH_TAG_TYPE,view);
}
});
break;
case 1: // for @ type tags
rowView = inflater.inflate(R.layout.tag_item, null);
view = rowView;
holder.upIv = (ImageView) rowView.findViewById(R.id.up);
holder.downIv = (ImageView) rowView.findViewById(R.id.down);
holder.neutralIv = (ImageView) rowView.findViewById(R.id.neutral);
holder.exclamationIv = (ImageView) rowView.findViewById(R.id.exclamation);
holder.upIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getTagStatus("up", position, holder,AT_TAG_TYPE,view);
}
});
holder.downIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getTagStatus("down", position, holder,AT_TAG_TYPE,view);
}
});
holder.neutralIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getTagStatus("neutral", position, holder,AT_TAG_TYPE,view);
}
});
holder.exclamationIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getTagStatus("esclamation", position, holder,AT_TAG_TYPE,view);
}
});
break;
}
holder.title = (TextView) rowView.findViewById(R.id.row_title);
holder.subtitle = (TextView) rowView.findViewById(R.id.row_subtitle);
rowView.setTag(holder);
}else {
holder = (Holder) rowView.getTag();
}
holder.title.setText(arrayList.get(position).getTitle());
holder.subtitle.setText(arrayList.get(position).getSubtitle());
if(viewType==1) {
if (arrayList.get(position).getTagstatus() != null) {
if (arrayList.get(position).getTagstatus().equals("up")) {
holder.upIv.setImageResource(R.drawable.up);
}
if (arrayList.get(position).getTagstatus().equals("down")) {
holder.downIv.setImageResource(R.drawable.down);
}
if (arrayList.get(position).getTagstatus().equals("neutral")) {
holder.neutralIv.setImageResource(R.drawable.neutral);
}
if (arrayList.get(position).getTagstatus().equals("esclamation")) {
holder.exclamationIv.setImageResource(R.drawable.exclamation);
}
}
}else
{
if(arrayList.get(position).getTagstatus()!=null)
if(arrayList.get(position).getTagstatus().equals("true"))
{
holder.checked.setImageResource(R.drawable.tick_icon);
}
}
return rowView;
}
public static class Holder {
TextView title,subtitle;
ImageView upIv,downIv,neutralIv,exclamationIv,checked;
RelativeLayout relativeLayout;
}
@Override
public Filter getFilter() {
return new ListFilter();
}
public class ListFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String constraintStr = constraint.toString().toLowerCase(Locale.getDefault());
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<TagDataModel> filterItems = new ArrayList<TagDataModel>();
synchronized (this) {
for (TagDataModel item : arrayList) {
if (item.getTitle().toLowerCase(Locale.getDefault()).startsWith(constraintStr)) {
filterItems.add(item);
}
}
result.count = filterItems.size();
result.values = filterItems;
}
} else {
synchronized (this) {
result.count = arrayList.size();
result.values = arrayList;
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filterSearchListener.UpdateFilteredResult(constraint, results.values);
}
}
public interface FilterSearchListener{
public void UpdateFilteredResult(CharSequence constraint, Object results);
}
public void getTagStatus(String status ,int position,Holder holder,int tagViewType,View view)
{
switch (tagViewType) {
case 0:
if(arrayList.get(position).getTagstatus()=="true") {
if (tagDatabaseHelper.deleteTag(arrayList.get(position)) > 0) {
arrayList.remove(arrayList.get(position));
notifyDataSetChanged();
}
}else
{
holder.checked.setImageResource(R.drawable.tick_icon);
arrayList.get(position).setTagstatus("true");
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
if (tagType.contains("All_Tags"))
tagActivity.setVisibilitySelectedTagListView();
break;
case 1:
switch (status) {
case "up":
selectedTag = "up";
if (arrayList.get(position).getTagstatus() != null) {
if (arrayList.get(position).getTagstatus().equals(selectedTag)) {
if (tagDatabaseHelper.deleteTag(arrayList.get(position)) > 0) {
arrayList.remove(arrayList.get(position));
notifyDataSetChanged();
}
} else {
holder.upIv.setImageResource(R.drawable.up);
holder.downIv.setImageResource(R.drawable.down_normal);
holder.neutralIv.setImageResource(R.drawable.neutral_normal);
holder.exclamationIv.setImageResource(R.drawable.exclamation_normal);
/*tag.setId(SelectTagActivity.selectedList.get(position).getId());
tag.setTitle(SelectTagActivity.selectedList.get(position).getTitle());
tag.setSubtitle(SelectTagActivity.selectedList.get(position).getSubtitle());*/
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
} else {
holder.upIv.setImageResource(R.drawable.up);
holder.downIv.setImageResource(R.drawable.down_normal);
holder.neutralIv.setImageResource(R.drawable.neutral_normal);
holder.exclamationIv.setImageResource(R.drawable.exclamation_normal);
/*tag.setId(SelectTagActivity.selectedList.get(position).getId());
tag.setTitle(SelectTagActivity.selectedList.get(position).getTitle());
tag.setSubtitle(SelectTagActivity.selectedList.get(position).getSubtitle());*/
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
if (tagType.contains("All_Tags"))
tagActivity.setVisibilitySelectedTagListView();
break;
case "down":
selectedTag = "down";
if (arrayList.get(position).getTagstatus() != null) {
if (arrayList.get(position).getTagstatus().equals(selectedTag)) {
if (tagDatabaseHelper.deleteTag(arrayList.get(position)) > 0) {
arrayList.remove(arrayList.get(position));
notifyDataSetChanged();
}
} else {
holder.upIv.setImageResource(R.drawable.up_normal);
holder.downIv.setImageResource(R.drawable.down);
holder.neutralIv.setImageResource(R.drawable.neutral_normal);
holder.exclamationIv.setImageResource(R.drawable.exclamation_normal);
/*tag.setId(SelectTagActivity.selectedList.get(position).getId());
tag.setTitle(SelectTagActivity.selectedList.get(position).getTitle());
tag.setSubtitle(SelectTagActivity.selectedList.get(position).getSubtitle());*/
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
} else {
holder.upIv.setImageResource(R.drawable.up_normal);
holder.downIv.setImageResource(R.drawable.down);
holder.neutralIv.setImageResource(R.drawable.neutral_normal);
holder.exclamationIv.setImageResource(R.drawable.exclamation_normal);
/*tag.setId(SelectTagActivity.selectedList.get(position).getId());
tag.setTitle(SelectTagActivity.selectedList.get(position).getTitle());
tag.setSubtitle(SelectTagActivity.selectedList.get(position).getSubtitle());*/
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
if (tagType.contains("All_Tags"))
tagActivity.setVisibilitySelectedTagListView();
break;
case "neutral":
selectedTag = "neutral";
if (arrayList.get(position).getTagstatus() != null) {
if (arrayList.get(position).getTagstatus().equals(selectedTag)) {
if (tagDatabaseHelper.deleteTag(arrayList.get(position)) > 0) {
arrayList.remove(arrayList.get(position));
notifyDataSetChanged();
}
} else {
**holder.upIv.setImageResource(R.drawable.up_normal);
holder.downIv.setImageResource(R.drawable.down_normal);
holder.neutralIv.setImageResource(R.drawable.neutral);
holder.exclamationIv.setImageResource(R.drawable.exclamation_normal);**
/*tag.setId(SelectTagActivity.selectedList.get(position).getId());
tag.setTitle(SelectTagActivity.selectedList.get(position).getTitle());
tag.setSubtitle(SelectTagActivity.selectedList.get(position).getSubtitle());*/
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
} else {
****holder.upIv.setImageResource(R.drawable.up_normal);
holder.downIv.setImageResource(R.drawable.down_normal);
holder.neutralIv.setImageResource(R.drawable.neutral);
holder.exclamationIv.setImageResource(R.drawable.exclamation_normal);
/*tag.setId(SelectTagActivity.selectedList.get(position).getId());
tag.setTitle(SelectTagActivity.selectedList.get(position).getTitle());
tag.setSubtitle(SelectTagActivity.selectedList.get(position).getSubtitle());*/
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
if (tagType.contains("All_Tags"))
tagActivity.setVisibilitySelectedTagListView();
break;
case "esclamation":
selectedTag = "esclamation";
if (arrayList.get(position).getTagstatus() != null) {
if (arrayList.get(position).getTagstatus().equals(selectedTag)) {
if (tagDatabaseHelper.deleteTag(arrayList.get(position)) > 0) {
arrayList.remove(arrayList.get(position));
notifyDataSetChanged();
}
} else {
holder.upIv.setImageResource(R.drawable.up_normal);
holder.downIv.setImageResource(R.drawable.down_normal);
holder.neutralIv.setImageResource(R.drawable.neutral_normal);
holder.exclamationIv.setImageResource(R.drawable.exclamation);
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
} else {
holder.upIv.setImageResource(R.drawable.up_normal);
holder.downIv.setImageResource(R.drawable.down_normal);
holder.neutralIv.setImageResource(R.drawable.neutral_normal);
holder.exclamationIv.setImageResource(R.drawable.exclamation);
/*tag.setId(SelectTagActivity.selectedList.get(position).getId());
tag.setTitle(SelectTagActivity.selectedList.get(position).getTitle());
tag.setSubtitle(SelectTagActivity.selectedList.get(position).getSubtitle());*/
arrayList.get(position).setTagstatus(selectedTag);
tagDatabaseHelper.addUpdateTag(arrayList.get(position));
}
if (tagType.contains("All_Tags"))
tagActivity.setVisibilitySelectedTagListView();
break;
}
break;
}
}
}
这是我的logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
at com.analec.clientmanager.adapter.SelectTagAdapter.getTagStatus(SelectTagAdapter.java:369)
at com.abc.manager.adapter.SelectTagAdapter$3.onClick(SelectTagAdapter.java:139)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
以粗体显示空指针异常的代码。