我有一个使用 ArrayAdapter 生成的 Lsit 视图。getView 方法中的 TextView 字段在使用 POJO 类中的值填充时引发错误。您能否让我知道我的代码需要更正的地方。
提前致谢。
public class ApplianceAdapter extends ArrayAdapter<POJO_Appliance> {
private ArrayList<POJO_Appliance> appliance;
Context context;
LayoutInflater vi;
public ApplianceAdapter(Context context ,ArrayList<POJO_Appliance> appliance) {
super(context, 0, appliance);
this.appliance = new ArrayList<POJO_Appliance>();
this.appliance.addAll(appliance);
this.context =context;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private class ViewHolder {
TextView applianceName;
Button cancel,edit;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = vi.inflate(R.layout.rowappliance, null);
holder.applianceName = (TextView) convertView.findViewById(R.id.tvapplianceName);
holder.cancel = (Button) convertView.findViewById(R.id.bCancel);
holder.edit = (Button) convertView.findViewById(R.id.bEdit);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Log.i("AddAppliance"," Appliance Name : " + appliance.get(position).getApplianceName());
holder.applianceName.setText(appliance.get(position).getApplianceName());
// Cancel button
holder.cancel.setTag(position);
holder.cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
int pos = (Integer) v.getTag();
Log.i("ConfirmAdapter ","Order cancel @ position : " + pos);
entry.deleteAppliance(roomName,appliance.get(pos).getApplianceName());
refreshAdapter();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
return convertView;
}
}
错误日志:
01-18 17:28:32.259: E/AndroidRuntime(28641): FATAL EXCEPTION: main
01-18 17:28:32.259: E/AndroidRuntime(28641): java.lang.NullPointerException
01-18 17:28:32.259: E/AndroidRuntime(28641): at com.example.homeauto.AddAppliance$ApplianceAdapter.getView(AddAppliance.java:136)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.AbsListView.obtainView(AbsListView.java:2040)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.makeAndAddView(ListView.java:1772)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.fillDown(ListView.java:672)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.fillFromTop(ListView.java:732)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.ListView.layoutChildren(ListView.java:1611)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.AbsListView.onLayout(AbsListView.java:1870)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1641)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1417)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.View.layout(View.java:11418)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewGroup.layout(ViewGroup.java:4328)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.os.Looper.loop(Looper.java:137)
01-18 17:28:32.259: E/AndroidRuntime(28641): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-18 17:28:32.259: E/AndroidRuntime(28641): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 17:28:32.259: E/AndroidRuntime(28641): at java.lang.reflect.Method.invoke(Method.java:511)
01-18 17:28:32.259: E/AndroidRuntime(28641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-18 17:28:32.259: E/AndroidRuntime(28641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-18 17:28:32.259: E/AndroidRuntime(28641): at dalvik.system.NativeStart.main(Native Method)