Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Listview RSS阅读器崩溃_Java_Android_Listview_Webview_Crash - Fatal编程技术网

Java Listview RSS阅读器崩溃

Java Listview RSS阅读器崩溃,java,android,listview,webview,crash,Java,Android,Listview,Webview,Crash,我正在尝试制作阅读rss提要的应用程序。我制作了一个应用程序,但它不会加载提要。我会给你们代码,这样你们可以告诉我我可以改变什么,使应用程序正常工作。我不知道是什么问题。代码中没有错误,但它不会加载提要 ListActivity.java: package com.listview; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.H

我正在尝试制作阅读rss提要的应用程序。我制作了一个应用程序,但它不会加载提要。我会给你们代码,这样你们可以告诉我我可以改变什么,使应用程序正常工作。我不知道是什么问题。代码中没有错误,但它不会加载提要

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));

}
}
}
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;
}

}
EfficientAdapter.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));

}
}
}
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;
}

}
package 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{
私人活动;
私有数组列表数据;
专用静态充气机=空;
//公共图像加载器;
视窗座;
效率适配器(活动a,阵列列表d){
活动=a;
数据=d;
充气器=(充气器)活动
.getSystemService(上下文布局\充气机\服务);
//imageLoader=新的imageLoader(activity.getApplicationContext());
}
@凌驾
public int getCount(){
返回数据.toArray().length;
}
@凌驾
公共对象getItem(int位置){
返回位置;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
公共静态类视图持有者{
公共文本视图标签;
公共文本视图地址;
公众形象;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null){
vi=充气机充气(R.layout.row,空);
holder=新的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=(ViewHolder)vi.getTag();
holder.label.setText(data.get(position.getTitle());
holder.addr.setText(data.get(position.getPubDate());
imageLoader.DisplayImage((data.get(position.getThumbnail()),活动,
图像,72,72);
URL=null;
试一试{
url=新url((data.get(position.getThumbnail());
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
InputStream内容=null;
试一试{
content=(InputStream)url.getContent();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
Drawable d=Drawable.createFromStream(内容,“src”);
位图mIcon1=null;
试一试{
mIcon1=
BitmapFactory.decodeStream(url.openConnection().getInputStream());
}捕获(IOE异常){
e、 printStackTrace();
}
holder.image.setImageBitmap(Bitmap.createScaledBitmap(mIcon1,72,72,false));
返回vi;
}
}
活动列表.xml:

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;
}

}
<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>
<?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>

row.xml:

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;
}

}
<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>
<?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>

日志


这应该从
http
方案开始(
http://

这是太多的代码了。试着把它缩小到一个能显示你的问题的范围。你应该发布崩溃跟踪。如果不这样做,就不可能在getCount()中跟踪问题,您可以在ArrayList对象上使用size()而不是toArray().lenght