我正在努力尝试阅读RSS并简单地在ListView上显示结果。我的应用程序运行然后崩溃。我认为这是因为我为列表适配器创建了“theAdapter”类。
public class MainActivity extends Activity {
ArrayList<handler> PostList=new ArrayList<handler>(1000);
ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.layout.activity_main);
new loading().execute("http://theurlfeed");
}
class loading extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
try {
helper.parse(params[0]);
} catch (IOException e) { e.printStackTrace();}
catch (XmlPullParserException e) {e.printStackTrace();}
return null;
}
protected void onPostExecute(String s) {
list.setAdapter(new theAdapter(MainActivity.this, PostList));
}
}}
public class handler {
String title="";
handler(String s){
title=s;
}
String getTitle(){
return title;
}}
读取和解析我的xml的类
public class helper {
public static ArrayList<handler> parse(String url) throws IOException, XmlPullParserException {
final ArrayList<handler> results = new ArrayList<handler>(1000);
String die=url;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
URL input = new URL(die);
xpp.setInput(input.openStream(), null);
int eventType = xpp.getEventType();
String currentTag = null;
Integer id = null;
String title = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
currentTag = xpp.getName();
} else if (eventType == XmlPullParser.TEXT) {
if ("id".equals(currentTag)) {
id = Integer.valueOf(xpp.getText());
}
if ("title".equals(currentTag)) {
title = xpp.getText();
}
} else if (eventType == XmlPullParser.END_TAG) {
if ("recipe".equals(xpp.getName())) {
results.add(new handler(title));
}
}
eventType = xpp.next();
}
return results;
}}
列表适配器
public class theAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<handler> data;
private static LayoutInflater inflater = null;
ViewHolder holder;
theAdapter(Activity a, ArrayList<handler> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
public TextView label;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.text, null);
holder = new ViewHolder();
holder.label = (TextView) vi.findViewById(R.id.title);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.label.setText(data.get(position).getTitle());
return vi;
}
public int getCount() {
return 0;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}}
这是日志。
11-16 17:49:39.920:W / dalvikvm(1627):threadid = 1:线程退出时未捕获异常(group = 0x40a13300) 11-16 17:49:39.940:E / AndroidRuntime(1627):致命异常:主要 11-16 17:49:39.940:E / AndroidRuntime(1627):java.lang.NullPointerException 11-16 17:49:39.940:E / AndroidRuntime(1627):at com.example.neo.MainActivity $ loading.onPostExecute(MainActivity.java:46) 11-16 17:49:39.940:E / AndroidRuntime(1627):at com.example.neo.MainActivity $ loading.onPostExecute(MainActivity.java:1) 11-16 17:49:39.940:E / AndroidRuntime(1627):在android.os.AsyncTask.finish(AsyncTask.java:631) 11-16 17:49:39.940:E / AndroidRuntime(1627):在android.os.AsyncTask.access $ 600(AsyncTask.java:177) 11-16 17:49:39.940:E / AndroidRuntime(1627):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644) 11-16 17:49:39.940:E / AndroidRuntime(1627):在android.os.Handler.dispatchMessage(Handler.java:99) 11-16 17:49:39.940:E / AndroidRuntime(1627):在android.os.Looper.loop(Looper.java:137) 11-16 17:49:39.940:E / AndroidRuntime(1627):在android.app.ActivityThread.main(ActivityThread.java:4745) 11-16 17:49:39.940:E / AndroidRuntime(1627):at java.lang.reflect.Method.invokeNative(Native Method) 11-16 17:49:39.940:E / AndroidRuntime(1627):at java.lang.reflect.Method.invoke(Method.java:511) 11-16 17:49:39.940:E / AndroidRuntime(1627):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 11-16 17:49:39.940:E / AndroidRuntime(1627):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-16 17:49:39.940:E / AndroidRuntime(1627):at dalvik.system.NativeStart.main(Native Method) 11-16 17:49:42.780:I / Process(1627):发送信号。 PID:1627 SIG:9
我错过了什么?