我是使用 java 编程的新手。
只是想弄清楚为什么我的代码不起作用。
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xpp.getName().equalsIgnoreCase("item")){
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText());**boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xpp.getName().equalsIgnoreCase("item")){
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText());
它给了我以下错误:
List 类型中的方法 add(capture#3-of ?) 不适用于参数 (String)
知道如何解决这个问题吗?有人可以向我解释为什么它不起作用吗?
编辑:
继承人整个 listActivity 代码:
public class Main_Menu extends ListActivity {
List<?> headlines;
List<?> links;
@SuppressWarnings("rawtypes")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__menu);
headlines = new ArrayList();
links = new ArrayList();
try{
URL url = new URL ("http://feeds.ign.com/ign/all");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xpp.getName().equalsIgnoreCase("item")){
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
headlines.add(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText());
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
@SuppressWarnings("unchecked")
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adapter);
}