我为listview_item构建了一个包含image,textview和textbox的适配器,我不知道为什么应用程序会抛出异常,
class RestaurantAdapter extends BaseAdapter {
private static LayoutInflater inflater = null;
private ArrayList<HashMap<String, String>> data;
Activity activity;
public RestaurantAdapter(Activity activity,
ArrayList<HashMap<String, String>> data) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.data = data;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null)
inflater.inflate(R.layout.restaurant_multi_select_list_item, null);
TextView name = (TextView) vi
.findViewById(R.id.restaurant_multi_select_title);
ImageView image = (ImageView) vi
.findViewById(R.id.restaurant_multi_select_list_item_image);
CheckBox cb = (CheckBox) vi
.findViewById(R.id.restaurant_multi_select_checkBox);
HashMap<String, String> restaurant = data.get(position);
name.setText(restaurant.get("name"));
cb.setSelected(false);
image.setId(Integer.parseInt(restaurant.get("image")));
return vi;
}
}
当我getview
包含此代码时
View vi = convertView;
if (vi == null)
inflater.inflate(R.layout.restaurant_multi_select_list_item, null);
它仍会抛出异常
如果你想要任何其他代码,我会把它给你
感谢
例外
01-23 19:45:27.021: E/AndroidRuntime(24106): FATAL EXCEPTION: main
01-23 19:45:27.021: E/AndroidRuntime(24106): java.lang.NullPointerException
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.AbsListView.obtainView(AbsListView.java:1521)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.ListView.makeAndAddView(ListView.java:1749)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.ListView.fillDown(ListView.java:674)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.ListView.fillFromTop(ListView.java:731)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.ListView.layoutChildren(ListView.java:1602)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.AbsListView.onLayout(AbsListView.java:1349)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.view.View.layout(View.java:7320)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1263)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1137)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.LinearLayout.onLayout(LinearLayout.java:1051)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.view.View.layout(View.java:7320)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.view.View.layout(View.java:7320)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1263)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1137)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.LinearLayout.onLayout(LinearLayout.java:1051)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.view.View.layout(View.java:7320)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.widget.FrameLayout.onLayout(FrameLayout.java:342)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.view.View.layout(View.java:7320)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.view.ViewRoot.performTraversals(ViewRoot.java:1162)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.view.ViewRoot.handleMessage(ViewRoot.java:1882)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.os.Looper.loop(Looper.java:130)
01-23 19:45:27.021: E/AndroidRuntime(24106): at android.app.ActivityThread.main(ActivityThread.java:3701)
01-23 19:45:27.021: E/AndroidRuntime(24106): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 19:45:27.021: E/AndroidRuntime(24106): at java.lang.reflect.Method.invoke(Method.java:507)
01-23 19:45:27.021: E/AndroidRuntime(24106): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
01-23 19:45:27.021: E/AndroidRuntime(24106): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
01-23 19:45:27.021: E/AndroidRuntime(24106): at dalvik.system.NativeStart.main(Native Method)
01-22 12:36:37.994: E/AndroidRuntime(318): FATAL EXCEPTION: main
01-22 12:36:37.994: E/AndroidRuntime(318): java.lang.NullPointerException
01-22 12:36:37.994: E/AndroidRuntime(318): at com.eattel.syriatel.RestaurantAdapter.getView(Select_Multi_Restaurants.java:113)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.widget.ListView.makeAndAddView(ListView.java:1727)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.widget.ListView.fillDown(ListView.java:652)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.widget.ListView.fillGap(ListView.java:623)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.widget.AbsListView.onTouchEvent(AbsListView.java:2065)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.widget.ListView.onTouchEvent(ListView.java:3315)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.view.View.dispatchTouchEvent(View.java:3766)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-22 12:36:37.994: E/AndroidRuntime(318): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
01-22 12:36:37.994: E/AndroidRuntime(318): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
01-22 12:36:37.994: E/AndroidRuntime(318): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.os.Looper.loop(Looper.java:123)
01-22 12:36:37.994: E/AndroidRuntime(318): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-22 12:36:37.994: E/AndroidRuntime(318): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 12:36:37.994: E/AndroidRuntime(318): at java.lang.reflect.Method.invoke(Method.java:521)
01-22 12:36:37.994: E/AndroidRuntime(318): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-22 12:36:37.994: E/AndroidRuntime(318): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-22 12:36:37.994: E/AndroidRuntime(318): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我想......
vi = inflater.inflate(R.layout.restaurant_multi_select_list_item, null);
答案 1 :(得分:1)
如果vi == null
则必须将inflater设置为vi
。否则你试图从任何地方获得一个视图或null
例如
if (vi == null)
vi = inflater.inflate(R.layout.restaurant_multi_select_list_item, null);