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)
user user
  • 2,122
  • 6
  • 19
  • 24

2 Answers2

1

I think...

vi = inflater.inflate(R.layout.restaurant_multi_select_list_item, null);
Mitsuaki Ishimoto
  • 3,162
  • 2
  • 25
  • 32
  • it works , but when i tried to scroll the items, i got an exception , why please? – user user Jan 22 '13 at 20:38
  • give me your stack trace? (Exception) – Mitsuaki Ishimoto Jan 22 '13 at 20:47
  • Thanks! Would you show me the line 133 of Select_Multi_Restaurants.java. – Mitsuaki Ishimoto Jan 22 '13 at 21:00
  • there is no 133 line :), it is just 119 – user user Jan 22 '13 at 21:03
  • And It may not be concerned with your problem. * If vi( == convertView ) is not null…, what is happen? * findViewById is called everytime when each list item data is assigned. This is not faster ListView. Check out following URL?(*´ω`*) http://stackoverflow.com/questions/14431420/i-need-to-add-a-imageview-or-webview-in-my-listview/14457392#14457392 – Mitsuaki Ishimoto Jan 22 '13 at 21:06
  • if it is not null , the app will contine from this `TextView name = (TextView) vi .findViewById(R.id.restaurant_multi_select_title);` this is not wrong i am sure of that, don't worry about null or not , it is 100% correct, i am confuse now why is exception when scroll the listview – user user Jan 22 '13 at 21:09
  • +1 for u , i hope i could accept more than one answer ,realy thank you – user user Jan 22 '13 at 21:23
1

if vi == null then you have to set the inflater to vi. otherwise you are trying to get a view from nothing or null

for example

if (vi == null)
       vi = inflater.inflate(R.layout.restaurant_multi_select_list_item, null);
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • thanks alot, +1 for u , i accept Mistsuaki's answer, because he answer me first – user user Jan 22 '13 at 20:32
  • same exception? if not post the new one – tyczj Jan 22 '13 at 20:39
  • @tyczjj i posted the new exception – user user Jan 22 '13 at 20:42
  • try not setting `vi` to `contentView` and just set `vi` to the `inflater view` like you would in the check real quick. Also where is line `113`? – tyczj Jan 22 '13 at 20:48
  • line 113 is this `image.setId(Integer.parseInt(restaurant.get("image")));` – user user Jan 22 '13 at 20:55
  • 1
    Either the values `restaurant` is null (but if its passing the `restaurant.get("name")` its probably not) or the data for the "image" is null or incorrect hard to tell from what is given, set a breakpoint in there and see what the image value is before passing it into the `ImageView` – tyczj Jan 22 '13 at 21:01
  • how to make a break point ? and how to see the image value then ? – user user Jan 22 '13 at 21:04
  • if you are in eclipse click on the line before that line and on the far left in the margin its blueish double click on that line and you will see a blue dot appear and thats a breakpoint. now start a new debugging session and it will stop at that line. If you are having problems getting it you will need to look it up but the problem is probably not anything related to your original question and has more to do with the data in there – tyczj Jan 22 '13 at 21:08
  • `image ImageView (id=830085571400)` i got this value – user user Jan 22 '13 at 21:13
  • also i got this `convertView null` – user user Jan 22 '13 at 21:14
  • i use this code from this site http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ now i rememberd that he use a class to show the image , have i used it too ? – user user Jan 22 '13 at 21:17
  • if that "image"value is the image resource id like `r.id.image` for example and all you are doing is trying to show an image the you are using the wrong method. you need to use `image.setImageResource(restaurant.get("image"));` if thats not the problem its best to start a new question as this is out of the scope of the origional – tyczj Jan 22 '13 at 21:23
  • yes baby :), setimageresourse works and the exception goes , thank youuuuuuuuuuuuuuuuuuuuuuuuu – user user Jan 22 '13 at 21:30
  • 1
    you can get to all the views in Window > show View and pick the view you want back – tyczj Jan 22 '13 at 21:31