在我的主要活动中,我调用了一个对象,编写了一个方法 itemClicked()。
public void itemClicked(){
System.out.println("Clicked");
efficienadapter.notifyDataSetChanged();
}
当我从列表视图中选择一行时,我正在尝试打印 Clicked 项目。注意:我正在使用一个名为 EfficientAdapter 的列表适配器。
这是适配器:
public class EfficientAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<ComptePost> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public Boolean isActusAstuce;
public static int flag = 0, counter=0;
NVirementEmmeteur main;
int num=0;
ViewHolder holder;
static String src;
public EfficientAdapter(Activity a, ArrayList<ComptePost> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader = new ImageLoader(activity.getApplicationContext());
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public EfficientAdapter(NVirementEmmeteur m) {
main=m;
}
@Override
public int getCount() {
return data.toArray().length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView one;
public TextView two;
public TextView three;
public ImageView image;
public RelativeLayout relative;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.single_item, null);
holder = new ViewHolder();
holder.one = (TextView) vi.findViewById(R.id.text_view_one);
holder.image = (ImageView)vi.findViewById(R.id.imageView1);
holder.relative = (Relativelayout)vi.findViewById(R.id.relative);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.one.setTextColor(Color.WHITE);
holder.one.setText("Compte No."+data.get(position).getNumComtpe());
holder.image.setVisibility(View.GONE);
holder.relative_layout.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.findViewById(R.id.imageView1).setVisibility(View.VISIBLE);
main.itemClicked();
}
});
//flag=0; only first row become white
return vi;
}
i used main.itemClicked() to print "Clicked".
holder.relative_layout.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.findViewById(R.id.imageView1).setVisibility(View.VISIBLE);
main.itemClicked();
}
});
但是当我选择一个行项目时,我得到了这个错误。
04-10 14:21:43.832: W/dalvikvm(4985): threadid=1: thread exiting with uncaught exception (group=0x40015578)
04-10 14:21:43.847: E/AndroidRuntime(4985): FATAL EXCEPTION: main
04-10 14:21:43.847: E/AndroidRuntime(4985): java.lang.NullPointerException
04-10 14:21:43.847: E/AndroidRuntime(4985): at .EfficientAdapter$1.onClick(EfficientAdapter.java:111)
04-10 14:21:43.847: E/AndroidRuntime(4985): at android.view.View.performClick(View.java:2538)
04-10 14:21:43.847: E/AndroidRuntime(4985): at android.view.View$PerformClick.run(View.java:9152)
04-10 14:21:43.847: E/AndroidRuntime(4985): at android.os.Handler.handleCallback(Handler.java:587)
04-10 14:21:43.847: E/AndroidRuntime(4985): at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 14:21:43.847: E/AndroidRuntime(4985): at android.os.Looper.loop(Looper.java:130)
04-10 14:21:43.847: E/AndroidRuntime(4985): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-10 14:21:43.847: E/AndroidRuntime(4985): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 14:21:43.847: E/AndroidRuntime(4985): at java.lang.reflect.Method.invoke(Method.java:507)
04-10 14:21:43.847: E/AndroidRuntime(4985): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-10 14:21:43.847: E/AndroidRuntime(4985): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-10 14:21:43.847: E/AndroidRuntime(4985): at dalvik.system.NativeStart.main(Native Method)
我在哪里弄错了?