我有一个列表视图,每行有 2 个图像,我希望图像在单击时更改,但每当我单击该项目时都会出现错误。我设置了一个祝酒词,以确认我单击了正确的项目并且有效。但最终我真正想要的是改变形象。关于如何正确设置它的任何建议?
07-08 15:19:43.371: E/AndroidRuntime(26277): FATAL EXCEPTION: main
07-08 15:19:43.371: E/AndroidRuntime(26277): java.lang.NullPointerException
07-08 15:19:43.371: E/AndroidRuntime(26277): at com.codalata.craigslistchecker.AutomationSettings$TheListAdapter2.onClick(AutomationSettings.java:217)
07-08 15:19:43.371: E/AndroidRuntime(26277): at android.view.View.performClick(View.java:2538)
07-08 15:19:43.371: E/AndroidRuntime(26277): at android.view.View$PerformClick.run(View.java:9158)
07-08 15:19:43.371: E/AndroidRuntime(26277): at android.os.Handler.handleCallback(Handler.java:587)
07-08 15:19:43.371: E/AndroidRuntime(26277): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 15:19:43.371: E/AndroidRuntime(26277): at android.os.Looper.loop(Looper.java:130)
07-08 15:19:43.371: E/AndroidRuntime(26277): at android.app.ActivityThread.main(ActivityThread.java:3704)
07-08 15:19:43.371: E/AndroidRuntime(26277): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 15:19:43.371: E/AndroidRuntime(26277): at java.lang.reflect.Method.invoke(Method.java:507)
07-08 15:19:43.371: E/AndroidRuntime(26277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
07-08 15:19:43.371: E/AndroidRuntime(26277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-08 15:19:43.371: E/AndroidRuntime(26277): at dalvik.system.NativeStart.main(Native Method)
public class TheListAdapter2 extends ArrayAdapter<Auto_Info> implements
OnClickListener {
public TheListAdapter2() {
super(AutomationSettings.this, R.layout.automationitems, Info);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View Auto_View = convertView;
ImageHolder holder = null;
if (Auto_View == null) {
Auto_View = getLayoutInflater().inflate(
R.layout.automationitems, parent, false);
holder = new ImageHolder();
holder.item_auto_title = (TextView) Auto_View
.findViewById(R.id.item_auto_title);
holder.item_auto_image1 = (ImageView) Auto_View
.findViewById(R.id.item_auto_image1);
holder.item_auto_image2 = (ImageView) Auto_View
.findViewById(R.id.item_auto_image2);
holder.item_auto_image1.setTag(R.id.item_auto_image1, holder);
holder.item_auto_image2.setTag(R.id.item_auto_image2, holder);
Auto_View.setTag(holder);
} else {
holder = (ImageHolder) Auto_View.getTag();
}
Auto_View.setOnClickListener(this);
Auto_Info CurrentItem = Info.get(position);
holder.CurrentItem = CurrentItem;
Typeface TitleFont = Typeface.createFromAsset(getAssets(),
"providence.ttf");
holder.item_auto_title.setTypeface(TitleFont);
holder.item_auto_title.setText(CurrentItem.getAUTONAME());
holder.item_auto_image1.setOnClickListener(this);
holder.item_auto_image2.setOnClickListener(this);
return Auto_View;
}
@Override
public void onClick(View v) {
ImageHolder holder = (ImageHolder) v.getTag();
if (v.getTag() == v.getTag(R.id.item_auto_image1)) {
Toast t = Toast.makeText(AutomationSettings.this, "blah",
Toast.LENGTH_LONG);
holder.item_auto_image1.setBackgroundResource(R.drawable.bstoppedrunning);
t.show();
} else if (v.getTag() == v.getTag(R.id.item_auto_image2)) {
Toast t = Toast.makeText(AutomationSettings.this, "blah2",
Toast.LENGTH_LONG);
t.show();
}
}
static class ImageHolder {
public Auto_Info CurrentItem;
ImageView item_auto_image1;
ImageView item_auto_image2;
TextView item_auto_title;
}
}