0

i build an adapter for a listview_item which contains image, textview and textbox, i don't know why the application throw an exception,

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;

    }
}

when i made the getview contains just this code

View vi = convertView;
            if (vi == null)
                inflater.inflate(R.layout.restaurant_multi_select_list_item, null);

it still throws the exception

if you want any another code i will give it to you

thanks

edit

exception

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)

the new exception

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)
4

2 回答 2

1

我认为...

vi = inflater.inflate(R.layout.restaurant_multi_select_list_item, null);
于 2013-01-22T20:28:38.437 回答
1

如果vi == null那么你必须将充气机设置为vi. 否则你试图从无到有或null

例如

if (vi == null)
       vi = inflater.inflate(R.layout.restaurant_multi_select_list_item, null);
于 2013-01-22T20:28:54.330 回答