0

我有一个看起来像这样的类:

public class Vizita {
     public int icon;
        public String titlu;
        public String loc;
        public String idviz;
        public Vizita(){
            super();
        }

        public Vizita(int icon, String titlu, String loc, String idviz) {
            super();
            this.icon = icon;
            this.titlu = titlu;
            this.loc = loc;
            this.idviz = idviz;
        }
}

我使用 AsyncTask 从 PHP 中检索 JSON 数组。所有的作品都非常适合列表视图的简单布局。但我想使用更复杂的布局,所以我设计了一个使用上面 Vizita.class 中的所有 4 个值的布局。但我不知道如何为其设置适配器,因为我尝试它的方式给了我错误。

当然,我正在尝试在 onPostExecute 中执行此操作,如下所示:

public class MyActivity extends Activity {
{...}
JSONArray lststat;
{...}
public JSONArray liststatus() {
    JSONArray jdata=post.getserverdataX(url_connectX);
    return jdata;
}   

class asyncpost extends AsyncTask< String, String, String > {
Vizita weather_data[];
....
protected void onPostExecute(String result) {
VizitaAdapter adapter = new VizitaAdapter(MyActivity.this,R.layout.listview_item_row, weather_data);
myListView.setAdapter(adapter);   
...}

在 doInBackground 我正在处理这样的 JSON:

lststat = liststatus();
JSONObject json_data; 
try {
     int length = lststat.length();
     Vizita weather_data[] = new Vizita[length];
     for (int i = 0; i < length; i++)
     {
        json_data = lststat.getJSONObject(i);
        weather_data[i].titlu =json_data.getString("clientnume");
        weather_data[i].idviz =json_data.getString("idvizite");
        weather_data[i].loc =json_data.getString("localitate");
     }
} catch (Exception e) {
Log.e(null, e.toString());
}            

其中 liststatus() 是我调用 asyncTask 以检索所需 JSONArray 的位置。

但是我在 for 循环中收到 java.lang.NullPointerException (如果我评论 setAdapter 行)。

我假设我没有正确初始化 weather_data 数组?!?!?如果我(取消注释)在 onPostExecute 中设置适配器,我会收到一个崩溃错误“强制关闭”,我在 IDE 中没有收到任何错误,看起来语法很好。

请告诉我我做错了什么?我该如何解决?

VizitaAdapter 的代码如下

public class VizitaAdapter extends ArrayAdapter<Vizita>{

    Context context;
    int layoutResourceId;   
    Vizita data[] = null;

    public VizitaAdapter(Context context, int layoutResourceId, Vizita[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtLoco = (TextView)row.findViewById(R.id.txtLocalitate);
            holder.txtIdviz = (TextView)row.findViewById(R.id.txtIdVizite);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        Vizita weather = data[position];
        holder.txtTitle.setText(weather.titlu);
        holder.txtLoco.setText(weather.loc);
        holder.txtIdviz.setText(weather.idviz);
        holder.imgIcon.setImageResource(weather.icon);

        return row;
    }

    static class WeatherHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtLoco;
        TextView txtIdviz;
    }    
}   
4

1 回答 1

2

可能你忘记了Vizita在循环中初始化对象:

json_data = lststat.getJSONObject(i);
weather_data[i] = new Vizita();
weather_data[i].titlu =json_data.getString("clientnume");
weather_data[i].idviz =json_data.getString("idvizite");
weather_data[i].loc =json_data.getString("localitate");
于 2013-07-31T13:46:54.040 回答