列表视图填充时出现NullPointerException

时间:2014-08-27 09:06:11

标签: java android android-listview custom-adapter

我有MainActivity类,我用JSOUP解析HTML页面,我也有自定义适配器,用于存储ListView中每行的数据。所以我用JSOUP获取数据并且它正常,因为我的LogCat告诉我。但是当我尝试将数据放到我的ListView时,我收到了错误。

我的 activity_main 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

我的 list_item 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/thumbImageNews"
        android:layout_marginLeft="10dp"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/titleNews"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/thumbImageNews"
        android:paddingLeft="5dp"
        android:paddingRight="0dp"
        android:paddingTop="5dp"
        android:text=""
        android:textColor="#ff0d075c"
        android:textStyle="italic"
        android:typeface="sans"
        android:gravity="center"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/date2News"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbImageNews"
        android:paddingLeft="5dp"
        android:paddingRight="0dp"
        android:paddingTop="2dp"
        android:text=""
        android:textColor="#ff585858"
        android:textSize="12sp"
        android:layout_marginLeft="10dp"/>

</RelativeLayout>

CustomListAdapter 活动:

public class CustomListAdapter extends BaseAdapter {

    private ArrayList<FeedItem> listData;
    private LayoutInflater layoutInflater;
    private Context mContext;

    public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        mContext = context;
    }

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

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

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) convertView.findViewById(R.id.titleNews);
            holder.reportedDateView = (TextView) convertView.findViewById(R.id.date2News);
            holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImageNews);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        FeedItem newsItem = (FeedItem) listData.get(position);
        holder.headlineView.setText(newsItem.getTitle());
 //     holder.reportedDateView.setText(newsItem.getDate());

        if (holder.imageView != null) {
            new ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl());
        }

        return convertView;
    }

    static class ViewHolder {
        TextView headlineView;
        TextView reportedDateView;
        ImageView imageView;
    }
}

FeedItem 类,用于存储每行的数据:

public class FeedItem implements Serializable {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    private String date;

    public String getAttachmentUrl() {
        return attachmentUrl;
    }

    public void setAttachmentUrl(String attachmentUrl) {
        this.attachmentUrl = attachmentUrl;
    }

    private String attachmentUrl;

}

我的 MainActivity 类执行所有主要功能:

public class MainActivity extends Activity {

    public Elements title;
    public Elements picture;

    final String ATTRIBUTE_NAME_TEXT = "text";
    final String ATTRIBUTE_NAME_IMAGE = "image";
    final String ATTRIBUTE_NAME_DATE = "date";

    private ArrayList<FeedItem> feedList = null;
    private ListView feedListView = null;
    public CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        feedListView = (ListView) findViewById(R.id.listView1);

        new NewThread().execute();

    }

public class NewThread extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... arg) {


    Document doc;
    try {

        doc = Jsoup.connect("http://example.com/news/").get();

        title = doc.select("h2[class=et_pt_title]");
        picture = doc.select("img");

       for (Element titles : title) {


           FeedItem item = new FeedItem();
           item.setTitle(titles.text());
           Log.w("title",""+item.getTitle());
            feedList.add(item);
        }
        for (Element img : picture){


            String iurl;
            iurl = img.absUrl("src");

            FeedItem item = new FeedItem();
            item.setAttachmentUrl(iurl);
            Log.w("imgUrl",""+item.getAttachmentUrl());
            feedList.add(item);


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

    return null;
}

@Override
protected void onPostExecute(String result) {

    adapter = new CustomListAdapter(MainActivity.this,feedList);
    feedListView.setAdapter(adapter);
}
}
 }

所以我的问题是当feedList.add(item);执行时我得到NullPointerException,但是Log.w("title",""+item.getTitle());告诉我一切正常。我做错了什么?

3 个答案:

答案 0 :(得分:2)

您的feedList未初始化。

所以在添加项目之前,请初始化。

ArrayList<FeedItem> feedList = new ArrayList<FeedItem> ;

在调用AsyncTask之前,即在new NewThread().execute();

之前添加上面的行

看起来应该是这样的

feedList = new ArrayList<FeedItem> ;
new NewThread().execute();

答案 1 :(得分:1)

private ArrayList<FeedItem> feedList = null;   

没有创建对象,您要在 arraylist

中添加数据
 feedList.add(item);

请创建对象

feedList = new ArrayList<FeedItem>()

而不是添加

feedList.add(item);

答案 2 :(得分:1)

你的错误是你刚宣布了这个

private ArrayList<FeedItem> feedList = null;

未初始化。所以只需初始化onCreate()方法。

feedList = new ArrayList<FeedItem> ;