在自定义列表视图中解析后,我编写了这段代码来显示新闻提要(RSS 提要).. 但我有一个异常,实际上我不知道问题出在哪里或如何解决这个异常!
编辑 :
我通过改变位置解决了这个问题:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,feedsTitles);
feedsList.setAdapter(new TempLyaout(MainActivity.this,feedsTitles));
代替:
adapter.setNotifyOnChange(true);
所以我得到了 RSS 提要并在自定义列表视图中显示它们 ^__^
MainActivity 类:
package com.example.testfeeds;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
@SuppressWarnings("unused")
public class MainActivity extends Activity {
String name;
Button getfeeds;
ArrayList<String> feedsTitles;
ArrayList<String> feedsLinks;
ListView feedsList;
ArrayAdapter<String> adapter;
ProgressDialog progress ;
@SuppressWarnings("null")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getfeeds = (Button) findViewById(R.id.button1);
feedsList=(ListView) findViewById(R.id.listView1);
progress = new ProgressDialog(this);
progress.setMessage("Loading .... ");
progress.setCancelable(false);
feedsTitles = new ArrayList<String>();
feedsLinks = new ArrayList<String>();
@SuppressWarnings("unchecked")
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,feedsTitles);
feedsList.setAdapter(new TempLyaout(MainActivity.this,feedsTitles));
getfeeds.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new GetFeeds().execute("http://yunn.yu.edu.jo/index.php?option=com_content&view=category&id=55&layout=blog&Itemid=104&format=feed&type=rss");
}
});
feedsList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(feedsLinks.get(position)));
startActivity(browserIntent);
}
});
}
////////////////////////////////////////////////////////////////////////////
class GetFeeds extends AsyncTask<String, Integer, String>{
@Override
protected void onPreExecute() {
progress.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
String xmlData = GetUrlBody(arg0[0]);
Log.d("msg","in doInBackground .. ");
return xmlData ;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("msg","the result not null");
////--------------------------------------////
//Log.d("msg",result);
FileOutputStream makeXml = null ; //declare a file to store the incoming xml data.
InputStream xmlFile = null; // declare an input stream to get the xml file.
try {
makeXml= openFileOutput("temp.xml", Context.MODE_PRIVATE);
makeXml.write(result.getBytes());
makeXml.flush();
makeXml.close();
xmlFile= openFileInput("temp.xml");
} catch (IOException e) {
e.printStackTrace();
}
////--------------------------------------////
XmlPullParserFactory pullParserFactory;
try {
pullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = pullParserFactory.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(xmlFile, null);
parseXML(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
progress.dismiss();
}
}
////////////////////////////////////////////////////////////////////////////
String GetUrlBody (String Url ){
Log.d("msg","in get url ..");
HttpClient cli = new DefaultHttpClient();
HttpGet g = new HttpGet(Url);
try{
HttpResponse res = cli.execute(g);
if(res.getStatusLine().getStatusCode() == 200){
String s =
EntityUtils.toString(res.getEntity(), HTTP.UTF_8);
return s;
}else {
return "Not Found";
}
}catch(Exception exx){}
return null;
}
////////////////////////////////////////////////////////////////////////////
private void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
int eventType = parser.getEventType();
int getElement=0;
String title,link,pubDate;
while (eventType != XmlPullParser.END_DOCUMENT){
String tagName = null;
switch (eventType){
//----------------------------------//
case XmlPullParser.START_DOCUMENT:
feedsTitles.clear();
feedsLinks.clear();
break;
//----------------------------------//
case XmlPullParser.START_TAG:
tagName = parser.getName();
if ("item".equals(tagName)){
getElement=1;
} else if (getElement==1){
if ("title".equals(tagName)){
title = parser.nextText();
Log.d("value",title);
feedsTitles.add(title);
} else if ("link".equals(tagName)){
link = parser.nextText();
Log.d("value",link);
feedsLinks.add(link);
} else if ("pubDate".equals(tagName)){
pubDate= parser.nextText();
Log.d("value",pubDate);
}
}
break;
//----------------------------------//
case XmlPullParser.END_TAG:
tagName = parser.getName();
if (tagName.equalsIgnoreCase("item") && getElement != 0){
}
break;
//----------------------------------//
}// end-switch.
eventType= parser.next();
}// end-while.
} //end xmlParser method.
////////////////////////////////////////////////////////////////////////////
} // end- MainActivity class.
自定义列表视图类:
package com.example.testfeeds;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class TempLyaout extends ArrayAdapter<String> {
Context context;
ArrayList<String> sa;
public TempLyaout(Context context , ArrayList<String> sa) {
super(context,R.layout.list_view,sa);
this.context=context;
this.sa = sa;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater l = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View rowView = l.inflate(R.layout.list_view, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.textView1);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView1);
textView.setText(sa.get(position));
return rowView;
}
}
堆栈跟踪:
07-27 09:55:40.866: D/AndroidRuntime(6035): Shutting down VM
07-27 09:55:40.866: W/dalvikvm(6035): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-27 09:55:40.940: E/AndroidRuntime(6035): FATAL EXCEPTION: main
07-27 09:55:40.940: E/AndroidRuntime(6035): java.lang.NullPointerException
07-27 09:55:40.940: E/AndroidRuntime(6035): at com.example.testfeeds.MainActivity.parseXML(MainActivity.java:286)
07-27 09:55:40.940: E/AndroidRuntime(6035): at com.example.testfeeds.MainActivity.access$0(MainActivity.java:227)
07-27 09:55:40.940: E/AndroidRuntime(6035): at com.example.testfeeds.MainActivity$GetFeeds.onPostExecute(MainActivity.java:169)
07-27 09:55:40.940: E/AndroidRuntime(6035): at com.example.testfeeds.MainActivity$GetFeeds.onPostExecute(MainActivity.java:1)
07-27 09:55:40.940: E/AndroidRuntime(6035): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-27 09:55:40.940: E/AndroidRuntime(6035): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-27 09:55:40.940: E/AndroidRuntime(6035): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-27 09:55:40.940: E/AndroidRuntime(6035): at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 09:55:40.940: E/AndroidRuntime(6035): at android.os.Looper.loop(Looper.java:123)
07-27 09:55:40.940: E/AndroidRuntime(6035): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-27 09:55:40.940: E/AndroidRuntime(6035): at java.lang.reflect.Method.invokeNative(Native Method)
07-27 09:55:40.940: E/AndroidRuntime(6035): at java.lang.reflect.Method.invoke(Method.java:521)
07-27 09:55:40.940: E/AndroidRuntime(6035): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-27 09:55:40.940: E/AndroidRuntime(6035): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-27 09:55:40.940: E/AndroidRuntime(6035): at dalvik.system.NativeStart.main(Native Method)