gpt4 book ai didi

java - ListView 填充时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:27:37 26 4
gpt4 key购买 nike

我有 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 Activity :

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()); 告诉我一切都很好。我做错了什么?

最佳答案

您的 feedList 尚未初始化。

因此,在向其中添加项目之前,请先进行初始化。

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

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

之前

它应该看起来像这样

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

关于java - ListView 填充时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com