【问题标题】:NullPointerException while listview fills列表视图填充时出现 NullPointerException
【发布时间】:2014-08-27 09:06:10
【问题描述】:

我有 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()); 告诉我一切正常。我做错了什么?

【问题讨论】:

  • 创建arraylist的对象而不是在arraylist中添加数据
  • 我认为您需要在“FeedItem”中添加更多字段。例如:item.setTitle、item.setDate.

标签: java android android-listview custom-adapter


【解决方案1】:

您的 feedList 未初始化。

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

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

在调用 AsyncTask 之前添加上述行,即在 new NewThread().execute(); 之前

应该是这样的

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

【讨论】:

    【解决方案2】:

    你的错误是你刚刚声明了这一点

    private ArrayList<FeedItem> feedList = null;
    

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

    feedList = new ArrayList<FeedItem> ;
    

    【讨论】:

    • 和我的答案一样。所以你可以对我的回答投赞成票。
    • 我不是在抄袭你。我知道答案,这个问题没有其他选择和答案。
    • 我从没说过你抄袭。当出现相同的答案时,为什么要再次发布?
    【解决方案3】:
    private ArrayList<FeedItem> feedList = null;   
    

    如果不创建 object,您将在 arraylist

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

    请创建对象

    feedList = new ArrayList<FeedItem>()
    

    比添加

    feedList.add(item);
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2018-08-13
      • 2013-11-26
      • 1970-01-01
      • 2014-01-05
      相关资源
      最近更新 更多