1

I have a listview adapter that I fill with json response. the response gets fine but when I set the adapter the app throw an error.

class with getters and setters:

  public class puntos {
  protected String name;
  protected int punto;
  protected String data;
  protected int nametwo;
  protected   Bitmap photo;


  public puntos(String name, int punto, int nametwo) {
    this.name = name;
    this.punto = punto;
    this.nametwo = nametwo;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  public int getNametwo() {
        return nametwo;
      }

      public void setNametwo(int nameTwo) {
        this.nametwo = nameTwo;
      }

  public int getPunto() {
    return punto;
  }

  public void setPunto(int punto) {
    this.punto = punto;
  }

  public String getData() {
    return data;
  }

  public void setData(String data) {
    this.data = data;
    try {   
      byte[] byteData = Base64.decode(data, Base64.DEFAULT);
      this.photo = BitmapFactory.decodeByteArray( byteData, 0, byteData.length);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public Bitmap getPhoto() {
    return photo;
  }
}

class adapter

public class puntosAdapter extends BaseAdapter {
  protected Activity activity;
  protected ArrayList<puntos> items;

  public puntosAdapter(Activity activity, ArrayList<puntos> items) {
    this.activity = activity;
    this.items = items;
  }

  @Override
  public int getCount() {
    return items.size();
  }

  @Override
  public Object getItem(int position) {
    return items.get(position);
  }

  @Override
  public long getItemId(int position) {
    return items.get(position).getNametwo();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;


      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      vi = inflater.inflate(R.layout.list_item_layout, null);


    puntos punt = items.get(position);

    ImageView image = (ImageView) vi.findViewById(R.id.pImage);
    image.setImageBitmap(punt.getPhoto());

    TextView name = (TextView) vi.findViewById(R.id.pName);
    name.setText(punt.getName());

    //TextView punto = (TextView) vi.findViewById(R.id.pPunto);
    //punto.setText(punt.getPunto());
    return vi;
  }
}

asynctask with httppost response

 class puntuar extends AsyncTask<Void, Void, Void>{
     String txt;
        @Override
        protected Void doInBackground(Void... params) {
             try {
                 HttpClient httpclient = new DefaultHttpClient();
                          HttpPost httppost = new HttpPost("http://comupunt.esy.es/puntos.php");
                          List<NameValuePair> parame = new ArrayList<NameValuePair>();
                         parame.add(new BasicNameValuePair("jug1", Integer.toString(1)));
                         parame.add(new BasicNameValuePair("jug2", Integer.toString(2)));
                          httppost.setEntity(new UrlEncodedFormEntity(parame));
                          HttpResponse resp = httpclient.execute(httppost);
                          HttpEntity ent = resp.getEntity();

                    txt = EntityUtils.toString(ent);
                    JSONObject jsonObject = new JSONObject(txt);
                    JSONArray cities = jsonObject.getJSONArray("cities");                 
                    for(int i = 0; i < cities.length(); i++) 
                     {
                        JSONObject city = cities.getJSONObject(i);
                        puntos c = new puntos(city.getString("name"), city.getInt("punto"), city.getInt("nametwo"));
                        c.setData(city.getString("photo"));
                        citiesAvaiable.add(c);
                        Log.d("error", String.valueOf(c.punto));
                     }
                 }catch(Exception e) {                
                    e.printStackTrace();
                    }
                    return null;
        }

        protected void onPostExecute(Void result) {
            try{
            puntosAdapter puntosAdapter = new puntosAdapter(puntosClass.this, citiesAvaiable);
            lv1.setAdapter(puntosAdapter);
                super.onPostExecute(result);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

and the error log

FATAL EXCEPTION: main
12-15 01:47:00.267: E/AndroidRuntime(5501): java.lang.NullPointerException
12-15 01:47:00.267: E/AndroidRuntime(5501):     at  
  com.example.images.puntosAdapter.getView(puntosAdapter.java:50)
 12-15 01:47:00.267: E/AndroidRuntime(5501):    at  
android.widget.AbsListView.obtainView(AbsListView.java:2267)
 E/AndroidRuntime(5501):    at 
android.widget.ListView.makeAndAddView(ListView.java:1769)
E/AndroidRuntime(5501):     at     
 android.widget.ListView.fillDown(ListView.java:672)
 E/AndroidRuntime(5501):    at    
android.widget.ListView.fillFromTop(ListView.java:733)
 E/AndroidRuntime(5501):    at    
android.widget.ListView.layoutChildren(ListView.java:1622)
 E/AndroidRuntime(5501):    at
android.widget.AbsListView.onLayout(AbsListView.java:2102)
 E/AndroidRuntime(5501):    at android.view.View.layout(View.java:13754)
 E/AndroidRuntime(5501):    at android.view.ViewGroup.layout(ViewGroup.java:4362)
 E/AndroidRuntime(5501):    at 
 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)    
  E/AndroidRuntime(5501):   at   
  android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
  E/AndroidRuntime(5501):   at 
  android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
  E/AndroidRuntime(5501):   at android.view.View.layout(View.java:13754)
  E/AndroidRuntime(5501):   at android.view.ViewGroup.layout(ViewGroup.java:4362)
  E/AndroidRuntime(5501):   at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
  E/AndroidRuntime(5501):   at android.view.View.layout(View.java:13754)
  E/AndroidRuntime(5501):   at android.view.ViewGroup.layout(ViewGroup.java:4362)
   E/AndroidRuntime(5501):  at 
  android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
   E/AndroidRuntime(5501):  at 
   android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
   E/AndroidRuntime(5501):  at 
   android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
   E/AndroidRuntime(5501):  at android.view.View.layout(View.java:13754)
   E/AndroidRuntime(5501):  at android.view.ViewGroup.layout(ViewGroup.java:4362)
   E/AndroidRuntime(5501):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    E/AndroidRuntime(5501):     at android.view.View.layout(View.java:13754)
   E/AndroidRuntime(5501):  at android.view.ViewGroup.layout(ViewGroup.java:4362)
   E/AndroidRuntime(5501):  at 
   android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
   E/AndroidRuntime(5501):  at
   android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
   E/AndroidRuntime(5501):  at 
   android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
   E/AndroidRuntime(5501):  at 
   android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
   E/AndroidRuntime(5501):  at 
   android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
   E/AndroidRuntime(5501):  at 
   android.view.Choreographer.doCallbacks(Choreographer.java:555)
    E/AndroidRuntime(5501):     at 
    android.view.Choreographer.doFrame(Choreographer.java:525)
   E/AndroidRuntime(5501):  at 
   android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
   E/AndroidRuntime(5501):  at android.os.Handler.handleCallback(Handler.java:615)
   E/AndroidRuntime(5501):  at android.os.Handler.dispatchMessage(Handler.java:92)
   E/AndroidRuntime(5501):  at android.os.Looper.loop(Looper.java:137)
   E/AndroidRuntime(5501):  at android.app.ActivityThread.main(ActivityThread.java:4745)
   E/AndroidRuntime(5501):  at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(5501):     at java.lang.reflect.Method.invoke(Method.java:511)
   E/AndroidRuntime(5501):  at 
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
   E/AndroidRuntime(5501):  at    
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    E/AndroidRuntime(5501):     at dalvik.system.NativeStart.main(Native Method)
dilux
  • 31
  • 5
  • 1
    Which is line 50 of puntosAdapter? – stkent Dec 15 '14 at 15:45
  • ImageView image = (ImageView) vi.findViewById(R.id.pImage); image.setImageBitmap(punt.getPhoto()); – dilux Dec 15 '14 at 15:49
  • I think you have errors in implementation of setData method in puntos class. – Artyom Kiriliyk Dec 15 '14 at 15:58
  • Is R.id.pImage definitely the correct id for your ImageView? – stkent Dec 15 '14 at 15:58
  • @stkent yes is the correct id – dilux Dec 15 '14 at 17:34
  • I am using the same method for this and works fine – dilux Dec 15 '14 at 17:35
  • Same method for what? If the NPE is in the two lines you indicated, either (1) vi is null, (2) image is null, or (3) punt is null. Can you debug to identify which of these is the case? – stkent Dec 15 '14 at 17:37
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – stkent Dec 15 '14 at 17:39
  • same method for fill other listview. I have just commented all of "setTexts and setImage" on "puntosAdapter" and works. but if I Log the response to the json the data is good. – dilux Dec 15 '14 at 17:42

2 Answers2

0

Because you use wrong way to inflate layout.

remove `

vi = inflater.inflate(R.layout.list_item_layout, null);

=

vi = inflater.inflate(R.layout.list_item_layout,parent,false);
hungkk
  • 338
  • 3
  • 11
0

ok, I have found the problem, I was calling the "list_item_layout" and the layout with my new listview was "lis_item2", this happened because I copy the method of other. thank you! an sorry

dilux
  • 31
  • 5