gpt4 book ai didi

android - RSS 提要空指针异常

转载 作者:行者123 更新时间:2023-11-30 02:39:41 25 4
gpt4 key购买 nike

基本上我创建了一个单独的 RSS,它可以工作。我为 ListView 外观使用了 DOMParsing 和自定义适配器。但现在我决定将该列表放在一个选项卡下,该选项卡表示 rss 提要的特定类别(即突发新闻、教育新闻等)。这是我的代码。

public class FragmentA extends ListFragment {

ListView listView;
ArrayList<RowItem> items;

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment


return inflater.inflate(R.layout.fragment_a, container, false);
}


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

listView = (ListView)getView().findViewById(R.id.list_item);
//listView.setOnItemClickListener(this);

// create the RowItem list - used for storing one news feed
items = new ArrayList<RowItem>();

// start the background task to get the news feed
//new DownloadXMLTask(items).execute("http://feeds.bbci.co.uk/news/rss.xml");

new DownloadXMLTask(items).execute("http://www.nasa.gov/rss/dyn/breaking_news.rss");
}


private class RowItem {
String title = null;
String description = null;
String url = null;

public RowItem(String title,String url,String description) {
this.title = title;
this.description = description;
this.url = url;
}




}

private class DownloadXMLTask extends AsyncTask<String, Void, String> {

ArrayList<RowItem> items = null;

public DownloadXMLTask(ArrayList<RowItem> items) {
this.items = items;
}

// local helper method
private String getNodeValue(Element entry, String tag) {
Element tagElement = (Element) entry.getElementsByTagName(tag).item(0);
return tagElement.getFirstChild().getNodeValue();
}

private String downloadAndParseXML(String url) {
try {
InputStream in = new java.net.URL(url).openStream();

// build the XML parser
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// parse and get XML elements
Document dom = db.parse(in);
Element documentElement = dom.getDocumentElement();

// we want all XML children called 'item'
NodeList nodes = documentElement.getElementsByTagName("item");

// for each 'item' do the following
for (int i = 0; i < nodes.getLength(); i++) {
Element entry = (Element) nodes.item(i);

// get the nodes from 'item' that you need
String title = getNodeValue(entry, "title");
//String description = getNodeValue(entry, "description");
String link = getNodeValue(entry, "link");
String description = getNodeValue(entry, "description");
// add them to your list
items.add(new RowItem(title,link,description));
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected String doInBackground(String... urls) {
if (urls.length <= 0)
return null;
return downloadAndParseXML(urls[0]);
}

protected void onPostExecute(String result) {
updateList(items);
}
}

public void updateList(ArrayList<RowItem> items) {
CustomArrayAdapter adapter = new CustomArrayAdapter(getActivity(),
R.layout.row, items);
listView.setAdapter(adapter);
}

// class used to have custom view for the list item
private class CustomArrayAdapter extends ArrayAdapter<RowItem> {
Context context;
List<RowItem> items;

private class ViewHolder {
public TextView title;
public TextView description;
}

public CustomArrayAdapter(Context context, int resource,
List<RowItem> items) {
super(context, resource, items);
this.context = context;
this.items = items;
}

@Override
public View getView(int position, View rowView, ViewGroup parent) {
// reuse the rowView if possible - for efficiency and less memory consumption
if (rowView == null) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) rowView.findViewById(R.id.title);
viewHolder.description = (TextView) rowView.findViewById(R.id.desc);

rowView.setTag(viewHolder);
}
// set the view
ViewHolder holder = (ViewHolder) rowView.getTag();
RowItem item = items.get(position);
holder.title.setText(item.title);
holder.description.setText(item.description);


return rowView;
}
}
}

我在 updatelist 方法中得到一个 NullPointer 异常,该方法用于更新检索到的数据列表(标题、ulr、链接)。显然它们不是,但我现在想不出如何修复它。

    09-21 16:07:41.310: E/AndroidRuntime(3678): FATAL EXCEPTION: main
09-21 16:07:41.310: E/AndroidRuntime(3678): java.lang.NullPointerException
09-21 16:07:41.310: E/AndroidRuntime(3678): at
com.example.tabstesting.FragmentA.updateList(FragmentA.java:159)
09-21 16:07:41.310: E/AndroidRuntime(3678):at

com.example.tabstesting.FragmentA$DownloadXMLTask.onPostExecute(FragmentA.java:152)

09-21 16:07:41.310: E/AndroidRuntime(3678): at
com.example.tabstesting.FragmentA$DownloadXMLTask.onPostExecute(FragmentA.java:1)
09-21 16:07:41.310: E/AndroidRuntime(3678): at
android.os.AsyncTask.finish(AsyncTask.java:631)

09-21 16:07:41.310: E/AndroidRuntime(3678): at
android.os.AsyncTask.access$600(AsyncTask.java:177)

预先感谢您,抱歉代码太长。

最佳答案

好的,所以我想我知道你的问题是什么。

fragment_a.xml 中,您将 ListView 声明为:

<ListView 
android:id="@android:id/list"
// ... rest of properties
</ListView>

第一个问题是你在说:

android:id="@android:id/list" 

这将使用 android 包中的 ID“list”,而不是您自己的资源。这就是为什么它在您的代码中无法识别的原因。此外,您尝试访问 ID 为 list_item 的元素,它甚至不存在于您的布局中,因此您得到一个 NullPointerException 因为 listView

因此,在获取 ListView 的代码中,您应该更改:

listView = (ListView)getView().findViewById(R.id.list_item);

收件人:

listView = (ListView)getView().findViewById(android.R.id.list);

关于android - RSS 提要空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963423/

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