来自JSON Parse的ListView有时候大多数都是null,什么会继续?

时间:2014-07-07 16:09:14

标签: android json nullpointerexception reddit

所以我的问题是我的适配器似乎为我从JSON源http://www.reddit.com/r/hot/.json?sort=new&count=25解析的大多数数据抛出了空例外。

我尝试过的事情: 1.我已经从解析中记录了所有正在创建的对象,以确认我获得了数据。 2.我在try catch语句中包含了有问题的代码,以找出它何时工作以及何时工作。

我学到了什么: 确实存在来自.json文档的数据 2.逻辑似乎只适用于列表视图中的三行,其余的返回null

我猜测也许某些json查询返回null,但我不知道为什么。

违规行为

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MainActivity.MyViewHolder holder;
    if (convertView == null){

        convertView = layoutInflater.inflate(R.layout.row, parent,false);
        holder = new MainActivity.MyViewHolder();
        holder.listName = (TextView)convertView.findViewById(R.id.title);
        holder.authorName = (TextView)convertView.findViewById(R.id.author);
        holder.thumbnail = (ImageView)convertView.findViewById(R.id.thumbnail);
        holder.goButton = (Button)convertView.findViewById(R.id.go_button);


    }else {
        holder = (MainActivity.MyViewHolder) convertView.getTag();
    }
    convertView.setOnClickListener(this);

    ListData data = topics.get(position);
    try {
Line81  holder.data = data;
        holder.listName.setText(data.getTitle());
        holder.authorName.setText(data.getAuthor());

        Log.v(DEBUG_TAG, "Cell Created");
    }catch (Exception e){
        e.printStackTrace();
        Log.v(DEBUG_TAG,"Cell Not Created Due to: ",e);
    }

    if(data.getImageUrl()!=null){
        try {
   Line93   holder.thumbnail.setTag(data.getImageUrl());


            Drawable drawable = imgGet.loadImage(this, holder.thumbnail);
            if (drawable != null) {
                holder.thumbnail.setImageDrawable(drawable);

            } else {
                holder.thumbnail.setImageResource(R.drawable.filler_icon);
            }
        }catch (Exception e){
            e.printStackTrace();
            Log.v(DEBUG_TAG,"no image: ",e);

        }

        return convertView;

错误讯息

07-07 10:45:27.396    6606-6606/com.google.android.gms.redditviewr.app V/LOGTAG!!!﹕ Cell Not Created Due to:
java.lang.NullPointerException
        at Adapters.RedditDataAdapter.getView(RedditDataAdapter.java:81)

07-07 10:45:27.404    6606-6606/com.google.android.gms.redditviewr.app V/LOGTAG!!!﹕ no image:
java.lang.NullPointerException
        at Adapters.RedditDataAdapter.getView(RedditDataAdapter.java:93)

我的JSON解析逻辑

try {
       JSONObject response = new JSONObject(result);
        JSONObject data = response.getJSONObject("data");
        JSONArray hotTopics = data.getJSONArray("children");
        Log.v(DEBUG_TAG, hotTopics.toString());
        for(int i=0; i<hotTopics.length(); i++) {
            JSONObject topic = hotTopics.getJSONObject(i).getJSONObject("data");
            ListData item = new ListData();
            item.setTitle(topic.getString("title"));
            item.setAuthor(topic.getString("author"));
            item.setImageUrl(topic.getString("thumbnail"));
            item.setPostTime(topic.getString("created_utc"));
            item.setrScore(topic.getString("score"));



                topicdata.add(item);
            Log.v(DEBUG_TAG,topicdata.toString());
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    this.activity.setTopics(topicdata);

}

1 个答案:

答案 0 :(得分:2)

修改:

if (convertView == null){

        convertView = layoutInflater.inflate(R.layout.row, parent,false);
        holder = new MainActivity.MyViewHolder();
        holder.listName = (TextView)convertView.findViewById(R.id.title);
        holder.authorName = (TextView)convertView.findViewById(R.id.author);
        holder.thumbnail = (ImageView)convertView.findViewById(R.id.thumbnail);
        holder.goButton = (Button)convertView.findViewById(R.id.go_button);

}

对此:

if (convertView == null) {

        convertView = layoutInflater.inflate(R.layout.row, parent,false);

        holder = new MainActivity.MyViewHolder();
        holder.listName = (TextView)convertView.findViewById(R.id.title);
        holder.authorName = (TextView)convertView.findViewById(R.id.author);
        holder.thumbnail = (ImageView)convertView.findViewById(R.id.thumbnail);
        holder.goButton = (Button)convertView.findViewById(R.id.go_button);

        convertView.setTag(holder);

}