1

我试图阅读 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;
}}

这是日志。

I/Process(1627):发送信号。PID:1627 SIG:9

我错过了什么?

4

0 回答 0