【问题标题】:NullPointer Exception when setting the adapter for a ListView为 ListView 设置适配器时出现 NullPointer 异常
【发布时间】:2012-04-02 13:03:54
【问题描述】:

我正在尝试通过 AsyncTask 将数据设置到适配器。这引起了很多悲伤 - 最近尝试设置阵列适配器时。

下面的方法调用onPostExecute();

private void setQueues(final JSONObject[] qInfo) 
{
    queues = new QueueItem[qInfo.length];

    for(int i = 0; i < qInfo.length; i++)
    {
        queues[i] = new QueueItem();
        //final int ii = i;

        // Formatting the queue title
        String name = qInfo[i].optString("name").replace("-", " ");
        queues[i].label = name;

        try {
            if(qInfo[i].getString("active").contains("1"))
            {
                queues[i].value = true;
            }
            else
            {
                queues[i].value = false;
            }
        } 
        catch (JSONException e) 
        {
            e.printStackTrace();
        }
    }
    lv.setAdapter(new QueueAdapter(getActivity(), 
            R.layout.queues_items, queues));

这会导致以下异常运行时:link here

编辑:根据要求,这里是 QueueAdapter:

public class QueueAdapter extends ArrayAdapter<QueueItem>{

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

public QueueAdapter(Context context, int layoutResourceId, QueueItem[] 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;
    QueueHolder holder = null;

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

        holder = new QueueHolder();
        holder.queueswitch = (Switch)row.findViewById(R.id.queues_item_switch);
        holder.txtLabel = (TextView)row.findViewById(R.id.queues_item_text);

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

    QueueItem queue = data[position];
    holder.txtLabel.setText(queue.label);
    holder.queueswitch.setChecked(queue.value);

    return row;
}

static class QueueHolder
{
    Switch queueswitch;
    TextView txtLabel;
}
}

【问题讨论】:

  • 检查QueuesFragment类行号98
  • 好像QueueAdapter有问题或者queues_items代码给代码
  • 你确定你在 ListView 上调用了 new 吗??
  • 我没有。我调用 lv = (ListView)getView().findViewById(R.id.queuesLayout);在 onActivityCreated() 中。

标签: android android-listview android-asynctask


【解决方案1】:
lv.setAdapter(new QueueAdapter(getActivity(), 
            R.layout.queues_items, queues));

这个 sn-p 应该在 try 块中。因为如果有 JSONException 该数组中的所有元素都将为空...

我的意思是说.. 将 for 循环放在 try 块中,而不是 oppt.. 如果您仍然想在发生异常时循环.. 然后尝试使用集合而不是数组..

【讨论】:

  • 是的。但是即使没有 JSONException 它仍然会导致空指针异常
  • @litemode .. 你需要确保该数组中的元素在任何情况下都不为空。你为什么不尝试集合而不是数组 .. 使用不允许空值..
  • 这不能回答我的问题。在这种情况下,qInfo 保存有效数据。 QueueItem 也是如此。
猜你喜欢
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多