我正在尝试在我的ListView
中获取RSS订阅源然后打开每个列表行的描述。我不知道为什么我的项目会关闭。我做错了什么?
这是我的代码:
MainActivity.java
public class MainActivity extends Activity {
// A reference to the local object
private MainActivity local;
/**
* This method creates main application view
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.activity_main);
// Set reference to this activity
local = this;
GetRSSDataTask task = new GetRSSDataTask();
// Start download RSS task
task.execute("http://www.conciencia.net/rss.aspx");
}
private class GetRSSDataTask extends AsyncTask<String, Void, List<RssAtomItem> > {
@Override
protected List<RssAtomItem> doInBackground(String... urls) {
try {
// Create RSS reader
RssAtomReader rssReader = new RssAtomReader(urls[0]);
// Parse RSS, get items
return rssReader.getItems();
} catch (Exception e) {
Log.e("ITCRssAtomReader", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(List<RssAtomItem> result) {
// Get a ListView from main view
ListView itcItems = (ListView) findViewById(R.id.listMainView);
// Create a list adapter
ArrayAdapter<RssAtomItem> adapter = new ArrayAdapter<RssAtomItem>(local,android.R.layout.simple_list_item_1, result);
// Set list adapter for the ListView
itcItems.setAdapter(adapter);
// Set list view item click listener
itcItems.setOnItemClickListener(new ListListener(result, local));
}
}
}
DetailsActivity.java
public class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
String title = (String)getIntent().getExtras().get("title");
String author = (String)getIntent().getExtras().get("author");
String guid = (String)getIntent().getExtras().get("guid");
String description = (String)getIntent().getExtras().get("description");
Log.d("DEBUG", "title:\t" + title);
Log.d("DEBUG", "author:\t" + author);
Log.d("DEBUG", "guid:\t" + guid);
Log.d("DEBUG", "description:\t\t" + description);
TextView titleTV = (TextView)findViewById(R.id.detailsTextView);
TextView titleTV2 = (TextView)findViewById(R.id.detailsTextView2);
TextView titleTV3 = (TextView)findViewById(R.id.detailsTextView3);
TextView titleTV4 = (TextView)findViewById(R.id.detailsTextView4);
titleTV.setText(title);
titleTV2.setText(title);
titleTV3.setText(title);
titleTV4.setText(title);
}
}
RssAtomItem.java
public class RssAtomItem {
private String title;
private String author;
private String guid;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return title;
}
}
ListListener.java
public class ListListener implements OnItemClickListener {
// List item's reference
List<RssAtomItem> listItems;
// Calling activity reference
Activity activity;
public ListListener(List<RssAtomItem> aListItems, Activity anActivity) {
listItems = aListItems;
activity = anActivity;
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
Intent i = new Intent(activity, DetailsActivity.class);
i.setData(Uri.parse(listItems.get(pos).getDescription()));
i.putExtra("title", listItems.get(pos).getTitle());
i.putExtra("author", listItems.get(pos).getAuthor());
i.putExtra("guid", listItems.get(pos).getGuid());
i.putExtra("description", listItems.get(pos).getDescription());
activity.startActivity(i);
}
}
RssAtomParseHandler.java
public class RssAtomParseHandler extends DefaultHandler {
private List<RssAtomItem> rssItems;
// Used to reference item while parsing
private RssAtomItem currentItem;
// Parsing title indicator
private boolean parsingTitle;
// Parsing link indicator
private boolean parsingContents;
// A buffer for title contents
private StringBuffer currentTitleSb;
// A buffer for content tag contents
private StringBuffer currentContentSb;
public RssAtomParseHandler() {
rssItems = new ArrayList<RssAtomItem>();
}
public List<RssAtomItem> getItems() {
return rssItems;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("entry".equals(qName)) {
currentItem = new RssAtomItem();
} else if ("title".equals(qName)) {
parsingTitle = true;
currentTitleSb = new StringBuffer();
} else if ("description".equals(qName)) {
parsingContents = true;
currentContentSb = new StringBuffer();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("entry".equals(qName)) {
rssItems.add(currentItem);
currentItem = null;
} else if ("title".equals(qName)) {
parsingTitle = false;
if (currentItem != null) // There is a title tag for a whole channel present. It is being parsed before the entry tag is present, so we need to check if item is not null
currentItem.setTitle(currentTitleSb.toString());
} else if ("description".equals(qName)) {
parsingContents = false;
if (currentItem != null)
currentItem.setDescription(currentContentSb.toString());
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (parsingTitle) {
if (currentItem != null)
currentTitleSb.append(new String(ch, start, length));
} else if (parsingContents) {
if (currentItem != null)
currentContentSb.append(new String(ch, start, length));
}
}
}
RssAtomReader.java
public class RssAtomReader {
private String rssUrl;
/**
* Constructor
*
* @param rssUrl
*/
public RssAtomReader(String rssUrl) {
this.rssUrl = rssUrl;
}
/**
* Get RSS items.
*
* @return
*/
public List<RssAtomItem> getItems() throws Exception {
// SAX parse RSS data
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
RssAtomParseHandler handler = new RssAtomParseHandler();
saxParser.parse(rssUrl, handler);
return handler.getItems();
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backgroundm"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android1:id="@+id/listMainView"
android1:layout_width="match_parent"
android1:layout_height="590dp"
android1:layout_alignParentTop="true"
android1:layout_centerHorizontal="true"
android1:layout_marginTop="125dp" >
</ListView>
</RelativeLayout>
activity_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/detailsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[POST TITLE GOES HERE]"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/detailsTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[POST AUTHOR GOES HERE]"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/detailsTextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[POST GUID GOES HERE]"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/detailsTextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="[POST DESCRIPTION GOES HERE]"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
这就是Logcat所说的:
01-14 03:38:55.933: D/gralloc_goldfish(5610): Emulator without GPU emulation detected.
01-14 03:38:56.623: E/(5610): At line 8, column 71: not well-formed (invalid token)
01-14 03:38:56.633: D/AndroidRuntime(5610): Shutting down VM
01-14 03:38:56.633: W/dalvikvm(5610): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-14 03:38:56.653: E/AndroidRuntime(5610): FATAL EXCEPTION: main
01-14 03:38:56.653: E/AndroidRuntime(5610): java.lang.NullPointerException
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.widget.ListView.setAdapter(ListView.java:462)
01-14 03:38:56.653: E/AndroidRuntime(5610): at net.con.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:75)
01-14 03:38:56.653: E/AndroidRuntime(5610): at net.con.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:1)
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.os.Looper.loop(Looper.java:137)
01-14 03:38:56.653: E/AndroidRuntime(5610): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-14 03:38:56.653: E/AndroidRuntime(5610): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 03:38:56.653: E/AndroidRuntime(5610): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 03:38:56.653: E/AndroidRuntime(5610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-14 03:38:56.653: E/AndroidRuntime(5610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-14 03:38:56.653: E/AndroidRuntime(5610): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在您的logcat中,以下行告诉您错误是什么
FATAL EXCEPTION: main
01-14 01:45:08.103: E/AndroidRuntime(3181): java.lang.NullPointerException
所以它是NPE
...某事是null
。引用您的项目之后的下一行告诉您异常发生的位置
at net.con.MainActivity$GetRSSDataTask.onPostExecute(MainActivity.java:73)
null
的{{1}}位于MainActivity
onPostExecute()
的{{1}}位置GetRSSDataTask
。
现在我们知道如何找到异常了,我们知道第73行是
itcItems.setAdapter(adapter);
因此您需要查看null
是什么以及可能导致此异常的原因。如果itcItems
不是null
,请设置断点并查看,然后您的adapter
可能是null
。
既然您已经知道如何阅读logcat,这将有助于您只发布最相关的代码。在这种情况下,其他类和xml文件不是必需的,因为MainActivity
中发生了异常。最初仅发布最相关的代码可以更容易地获得帮助。
答案 1 :(得分:0)
ArrayAdapter
null Pointer Exception
在setAdapter
尝试getCount()
时正在result
。这很可能是因为您未在onPostExecute
onPostExecute
中收到任何值
编辑:
result
使用doInBackground
返回的null
,在您的情况下返回result
。这意味着onPostExecute
中的null
为result
,您使用此null
构建了一个适配器,即null pointer exception
,这就是您获得adapter
的原因当您尝试使用count = 0
设置exception
时。这意味着您在try catch
块中获得null
,因此返回rssReader.getItems()
而不是返回{{1}};。