列表视图RSS阅读器崩溃 [英] Listview RSS reader crashes

查看:113
本文介绍了列表视图RSS阅读器崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让阅读RSS提要的应用程序。我做了应用程序,但它不会加载源。我会给你code所以你可以告诉我,我可以改变,以使应用程序正常工作。我不知道是什么问题。有一个在code没有错误,但它不会加载源。

ListActivity.java:

 包com.listview;

进口java.net.MalformedURLException;
进口的java.net.URL;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口javax.xml.parsers.SAXParser中;
进口javax.xml.parsers.SAXParserFactory中;

进口org.xml.sax.Attributes;
进口org.xml.sax.InputSource中;
进口org.xml.sax.SAXException;
进口org.xml.sax.XMLReader中;
进口org.xml.sax.helpers.DefaultHandler中;

进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.content.Intent;
进口android.net.Uri;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ListAdapter;
进口android.widget.ListView;

公共类ListActivity延伸活动{
ListView控件LV1;
ProgressDialog ShowProgress;
公众的ArrayList<邮政> PostList =新的ArrayList<邮政>();

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_list);

    LV1 =(ListView控件)findViewById(R.id.listView1);
    ShowProgress = ProgressDialog.show(ListActivity.this,,请耐心等待...,真正的);
    新loadingTask()执行(av-gurus.blogspot.com/feeds/posts/default/);

    lv1.setOnItemClickListener(新OnItemClickListener(){
        公共无效onItemClick(适配器视图<>母公司视图中查看,
                INT位置,长的id){

            意向意图=新的意图(Intent.ACTION_VIEW).setData(URI
                    .parse(PostList.get(位置).getUrl()));
            startActivity(意向);
        }
    });
}

类loadingTask扩展的AsyncTask<字符串,太虚,字符串> {

    @覆盖
    保护字符串doInBackground(字符串...网址){
        SAXHelper SH = NULL;
        尝试
        {
            的sh =新SAXHelper(网址[0]);
        }赶上(MalformedURLException异常E){
            e.printStackTrace();
        }
        sh.parseContent();
        返回 ;


        }
    保护无效onPostExecute(String s)将{
        lv1.setAdapter(新EfficientAdapter(ListActivity.this,PostList));
        ShowProgress.dismiss();
    }

}

类SAXHelper {
    公众的HashMap<字符串,字符串>用户列表=新的HashMap<字符串,字符串>();
    私人URL URL2;

    公共SAXHelper(字符串为url1)抛出MalformedURLException的{
        this.url2 =新的URL(URL1);
    }

    公众位于RSSHandler parseContent(字符串parseContent){
        位于RSSHandler DF =新位于RSSHandler();
        尝试 {

            的SAXParserFactory SPF = SAXParserFactory.newInstance();
            的SAXParser藻= spf.newSAXParser();
            XMLReader的XR = sp.getXMLReader();
            xr.setContentHandler(DF);
            xr.parse(新的InputSource(url2.openStream()));
        }赶上(例外五){
            e.printStackTrace();
        }
        返回DF;
    }
}

类位于RSSHandler扩展的DefaultHandler {
    私人邮政currentPost =新邮();
    StringBuffer的字符=新的StringBuffer();

    @覆盖
    公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
            属性的ATT){

        字符=新的StringBuffer();
        如果(localName.equalsIgnoreCase(项目)){

        }
    }
    @覆盖
    公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
            抛出的SAXException {

        如果(localName.equalsIgnoreCase(标题)
                &功放;&安培; currentPost.getTitle()== NULL){
            currentPost.setTitle(chars.toString());

        }
        如果(localName.equalsIgnoreCase(pubdate的)
                &功放;&安培; currentPost.getPubDate()== NULL){
            currentPost.setPubDate(chars.toString());

        }
        如果(localName.equalsIgnoreCase(缩略图)
                &功放;&安培; currentPost.getThumbnail()== NULL){
            currentPost.setThumbnail(chars.toString());

        }
        如果(localName.equalsIgnoreCase(链接)
                &功放;&安培; currentPost.getUrl()== NULL){
            currentPost.setUrl(chars.toString());
        }

        如果(localName.equalsIgnoreCase(项目)){
            PostList.add(currentPost);
            currentPost =新邮();
        }

    }

    @覆盖
    公共无效字符(字符CH [],诠释开始,诠释长度){
        chars.append(新的String(CH,开始,长度));

}
}
}
 

Post.java:

 包com.listview;

公共类帖子{

私人字符串名称;
私人字符串缩略图;
私人字符串URL;
私人字符串描述;
私人字符串pubdate的;

公共字符串的getTitle(){
    返回称号;
}

公共无效的setTitle(字符串名称){
    this.title =称号;
}

公共字符串getThumbnail(){
    返回缩略图;
}

公共无效setThumbnail(字符串缩略图){
    this.thumbnail =缩略图;
}

公共字符串的getURL(){
    返回URL;
}

公共无效setUrl(字符串URL){
    this.url =网址;
}

公共无效setDescription(字符串描述){
    this.description =描述;
}

公共字符串getDescription(){
    返回描述;
}

公共无效setPubDate(字符串pubdate的){
    this.pubDate = pubdate的;
}

公共字符串getPubDate(){
    返回pubdate的;
}

}
 

EfficientAdapter.java:

 包com.listview;

进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口的java.util.ArrayList;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.drawable.Drawable;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.ImageView;
进口android.widget.TextView;

公共类EfficientAdapter扩展了BaseAdapter {
私人活动的活动;
私人的ArrayList<邮政>数据;
私有静态LayoutInflater吹气= NULL;
//公共ImageLoader的ImageLoader的;
ViewHolder持有人;

EfficientAdapter(活动一,ArrayList的<邮政和GT; D){

    活性= A;
    数据= D;
    充气=(LayoutInflater)活动
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// ImageLoader的=新ImageLoader的(activity.getApplicationContext());

}

@覆盖
公众诠释getCount将(){
    返回data.toArray()的长度。

}

@覆盖
公共对象的getItem(INT位置){

    返回的位置;
}

@覆盖
众长getItemId(INT位置){

    返回的位置;
}

公共静态类ViewHolder {
    公众的TextView标签;
    公共TextView的地址;
    公众ImageView的形象;
}

@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    查看VI = convertView;

    如果(convertView == NULL){
        VI = inflater.inflate(R.layout.row,NULL);
        持有人=新ViewHolder();
        holder.label =(TextView中)vi.findViewById(R.id.title);
        holder.addr =(TextView中)vi.findViewById(R.id.details);
        holder.image =(ImageView的)vi.findViewById(R.id.thumb);
        vi.setTag(保持器);
    } 其他
        支架=(ViewHolder)vi.getTag();

    holder.label.setText(data.get(位置).getTitle());
    holder.addr.setText(data.get(位置).getPubDate());

    imageLoader.DisplayImage((data.get(位置).getThumbnail()),活动,
            holder.image,72,72);
    网址URL = NULL;
    尝试 {
        URL =新的URL((data.get(位置).getThumbnail()));
    }赶上(MalformedURLException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    InputStream的内容= NULL;
    尝试 {
        内容=(InputStream中)url.getContent();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    绘制对象D = Drawable.createFromStream(内容,SRC);
    位图mIcon1 = NULL;
     尝试 {
         mIcon1 =
                BitmapFactory.de codeStream(url.openConnection()的getInputStream());
    }赶上(IOException异常E){
        e.printStackTrace();
    }
    holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1,72,72,FALSE));


    返回六;
}

}
 

activity_list.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=。ListActivity>

<的ListView
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:scrollingCache =假
    机器人:后台=#FFFFFF
    机器人:cacheColorHint =#00000000
    机器人:ID =@ + ID / ListView1的>
< / ListView控件>

< / LinearLayout中>
 

row.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的机器人:ID =@ + ID / widget30
机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
机器人:填充=4dip的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
< TextView的机器人:ID =@ + ID /详细信息机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT机器人:文本=TextView的
    机器人:文字颜色=#000000机器人:layout_toRightOf =@ + ID /图标
    机器人:layout_alignLeft =@ + ID /标题机器人:layout_alignRight =@ + ID /标题
    机器人:layout_below =@ + ID /标题机器人:layout_marginRight =5px的>
< / TextView的>
< TextView的机器人:ID =@ + ID /标题机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT机器人:文本=TextView的
    机器人:TEXTSTYLE =黑体机器人:文字颜色=#000000
    机器人:layout_marginTop =5px的机器人:layout_toRightOf =@ + ID /图标
    机器人:layout_marginRight =5px的>
< / TextView的>
< ImageView的机器人:ID =@ + ID /箭头机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT机器人:填充=0px
    机器人:layout_marginRight =10px的机器人:SRC =@可绘制/ ic_launcher
    机器人:layout_alignRight =@ + ID /图标机器人:layout_alignParentRight =真
    机器人:layout_centerVertical =真正的>
< / ImageView的>
< ImageView的机器人:ID =@ + ID /拇指机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT机器人:layout_marginRight =10px的
    机器人:layout_centerVertical =真正的机器人:layout_alignParentLeft =真
    机器人:SRC =@可绘制/ ic_launcher机器人:=了minWidth72dip
    机器人:=了minHeight72dip机器人:=了maxWidth72dip机器人:=了maxHeight72dip>
< / ImageView的>

< / RelativeLayout的>
 

日志 http://pastebin.com/wnEZE92F

解决方案

 av-gurus.blogspot.com/feeds/posts/default/
 

这应该是与 HTTP 计划启动(的http://

I am trying to make application for reading rss feeds. I made app,but it wont load feeds. I will give you code so you can tell me what I can change to make app working properly. I don't know what the problem is. There is no errors in code but it wont load feeds.

ListActivity.java:

package com.listview;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ListActivity extends Activity {
ListView lv1;
ProgressDialog ShowProgress;
public ArrayList<Post> PostList = new ArrayList<Post>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    lv1 = (ListView)findViewById(R.id.listView1);
    ShowProgress = ProgressDialog.show(ListActivity.this,"","Loading. Please wait...",true);
    new loadingTask().execute("av-gurus.blogspot.com/feeds/posts/default/");

    lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,View view,
                int position, long id){

            Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri
                    .parse(PostList.get(position).getUrl()));
            startActivity(intent);
        }
    });
}

class loadingTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        SAXHelper sh = null;
        try 
        {
            sh = new SAXHelper(urls[0]);
        }catch (MalformedURLException e){
            e.printStackTrace();
        }
        sh.parseContent("");
        return "";


        }
    protected void onPostExecute(String s) {
        lv1.setAdapter(new EfficientAdapter(ListActivity.this, PostList));
        ShowProgress.dismiss();
    }

}

class SAXHelper{
    public HashMap<String, String> userList = new HashMap<String, String>();
    private URL url2;

    public SAXHelper(String url1) throws MalformedURLException {
        this.url2 = new URL(url1);
    }

    public RSSHandler parseContent(String parseContent) {
        RSSHandler df = new RSSHandler();
        try {

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            xr.setContentHandler(df);
            xr.parse(new InputSource(url2.openStream()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return df;
    }
}

class RSSHandler extends DefaultHandler {
    private Post currentPost = new Post();
    StringBuffer chars = new StringBuffer();

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) {

        chars = new StringBuffer();
        if (localName.equalsIgnoreCase("item")) {

        }
    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if (localName.equalsIgnoreCase("title")
                && currentPost.getTitle() == null) {
            currentPost.setTitle(chars.toString());

        }
        if (localName.equalsIgnoreCase("pubDate")
                && currentPost.getPubDate() == null) {
            currentPost.setPubDate(chars.toString());

        }
        if (localName.equalsIgnoreCase("thumbnail")
                && currentPost.getThumbnail() == null) {
            currentPost.setThumbnail(chars.toString());

        }
        if (localName.equalsIgnoreCase("link")
                && currentPost.getUrl() == null) {
            currentPost.setUrl(chars.toString());
        }

        if (localName.equalsIgnoreCase("item")) {
            PostList.add(currentPost);
            currentPost = new Post();
        }

    }

    @Override
    public void characters(char ch[], int start, int length) {
        chars.append(new String(ch, start, length));

}
}
}

Post.java:

package com.listview;

public class Post {

private String title;
private String thumbnail;
private String url;
private String description;
private String pubDate;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getThumbnail() {
    return thumbnail;
}

public void setThumbnail(String thumbnail) {
    this.thumbnail = thumbnail;
}

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}

public void setDescription(String description) {
    this.description = description;
}

public String getDescription() {
    return description;
}

public void setPubDate(String pubDate) {
    this.pubDate = pubDate;
}

public String getPubDate() {
    return pubDate;
}

}

EfficientAdapter.java:

package com.listview;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EfficientAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<Post> data;
private static LayoutInflater inflater = null;
//public ImageLoader imageLoader;
ViewHolder holder;

EfficientAdapter(Activity a, ArrayList<Post> d) {

    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//  imageLoader = new ImageLoader(activity.getApplicationContext());

}

@Override
public int getCount() {
    return data.toArray().length;

}

@Override
public Object getItem(int position) {

    return position;
}

@Override
public long getItemId(int position) {

    return position;
}

public static class ViewHolder {
    public TextView label;
    public TextView addr;
    public ImageView image;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;

    if (convertView == null) {
        vi = inflater.inflate(R.layout.row, null);
        holder = new ViewHolder();
        holder.label = (TextView) vi.findViewById(R.id.title);
        holder.addr = (TextView) vi.findViewById(R.id.details);
        holder.image = (ImageView) vi.findViewById(R.id.thumb);
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();

    holder.label.setText(data.get(position).getTitle());
    holder.addr.setText(data.get(position).getPubDate());

    imageLoader.DisplayImage((data.get(position).getThumbnail()), activity,
            holder.image, 72, 72);
    URL url = null;
    try {
        url = new URL((data.get(position).getThumbnail()));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    InputStream content = null;
    try {
        content = (InputStream)url.getContent();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Drawable d = Drawable.createFromStream(content , "src"); 
    Bitmap mIcon1 = null;
     try {
         mIcon1 =
                BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1, 72, 72, false));


    return vi;
}

}

activity_list.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ListActivity" >

<ListView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollingCache="false"
    android:background="#ffffff"
    android:cacheColorHint="#00000000"
    android:id="@+id/listView1">
</ListView>

</LinearLayout>

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/widget30"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="4dip" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/details" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="TextView"
    android:textColor="#000000" android:layout_toRightOf="@+id/icon"
    android:layout_alignLeft="@+id/title" android:layout_alignRight="@+id/title"
    android:layout_below="@+id/title" android:layout_marginRight="5px">
</TextView>
<TextView android:id="@+id/title" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="TextView"
    android:textStyle="bold" android:textColor="#000000"
    android:layout_marginTop="5px" android:layout_toRightOf="@+id/icon"
    android:layout_marginRight="5px">
</TextView>
<ImageView android:id="@+id/arrow" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:padding="0px"
    android:layout_marginRight="10px" android:src="@drawable/ic_launcher"
    android:layout_alignRight="@+id/icon" android:layout_alignParentRight="true"
    android:layout_centerVertical="true">
</ImageView>
<ImageView android:id="@+id/thumb" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_marginRight="10px"
    android:layout_centerVertical="true" android:layout_alignParentLeft="true"
    android:src="@drawable/ic_launcher" android:minWidth="72dip"
    android:minHeight="72dip" android:maxWidth="72dip" android:maxHeight="72dip">
</ImageView>

</RelativeLayout>

LOG http://pastebin.com/wnEZE92F

解决方案

"av-gurus.blogspot.com/feeds/posts/default/"

this should be start with the http scheme (http://)

这篇关于列表视图RSS阅读器崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆