我有一个listview适配器,我用json响应填充。响应变得很好但是当我设置适配器时,app会抛出一个错误。
带有getter和setter的类:
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;
}
}
类适配器
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;
}
}
assectask 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();
}
}
}
和错误日志
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)
答案 0 :(得分:0)
因为你使用错误的方式来扩充布局。
删除`
vi = inflater.inflate(R.layout.list_item_layout, null);
=
vi = inflater.inflate(R.layout.list_item_layout,parent,false);
答案 1 :(得分:0)