早上好
我想解析rss xml文件。我使用一个线程来实现这个操作,但通常应用程序崩溃,我收到了这条消息Java.lang.NullPointerException....
package com.example.albir;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.os.Bundle;
import android.widget.ListView;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private PostData[] listData;
int i =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listData =new PostData[100];
Thread thread = new Thread()
{
@Override
public void run() {
try {
PostData data = null;
URL url = new URL("http://######.org/?feed=rss2");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
data = new PostData();
data.postThumbUrl = null;
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
data.postTitle=xpp.nextText();
}
else if (xpp.getName().equalsIgnoreCase("pubDate")) {
if (insideItem)
data.postDate=xpp.nextText();
}
}
else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
listData[i] = data;
i++;
}
eventType = xpp.next(); //move to next element
}
// Binding data
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
thread.start();
Log.v("AAAA",String.valueOf(i));
for (int j=0;j<i;j++)
Log.v(listData[j].postTitle," ");
// Binding data
ListView listView = (ListView) this.findViewById(R.id.postListView);
PostItemAdapter itemAdapter = new PostItemAdapter(this,
R.layout.postitem, listData);
listView.setAdapter(itemAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
}
Slass postitem适配器
package com.example.albir;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class PostItemAdapter extends ArrayAdapter<PostData> {
private Activity myContext;
private PostData[] datas;
public PostItemAdapter(Context context, int textViewResourceId,
PostData[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
myContext = (Activity) context;
datas = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
if(convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.postitem, null);
}
rowView = convertView;
ImageView thumbImageView = (ImageView) rowView
.findViewById(R.id.postThumb);
if (datas[position].postThumbUrl == null) {
thumbImageView.setImageResource(R.drawable.ic_launcher);
}
TextView postTitleView = (TextView) rowView
.findViewById(R.id.postTitleLabel);
postTitleView.setText(datas[position].postTitle);
TextView postDateView = (TextView) rowView
.findViewById(R.id.postDateLabel);
postDateView.setText(datas[position].postDate);
return rowView;
}
}
我需要一个帮助来解析RSS feed而不会出现此问题,或者是否有更高效的其他解决方案。
答案 0 :(得分:0)
获取LayoutInflater对象的方法多于直接来自Activity的方法。事实上,getLayoutInflater()可能只是一个方便的方法: 你可以
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.postitem, parent, false);
或
LayoutInflater inflater = myContext.getLayoutInflater(null);
convertView = inflater.inflate(R.layout.postitem, parent, false);
而不是
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.postitem, null);
阅读doc
答案 1 :(得分:0)
将此用于inflater:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
使用适用于ViewHolder方法的适配器,效率更高