gpt4 book ai didi

java - 解析XML,我没有错误,我不知道出了什么问题?

转载 作者:行者123 更新时间:2023-12-01 13:38:01 24 4
gpt4 key购买 nike

我试图在我的 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));
}
}

}

详细信息Activity.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)

最佳答案

在您的 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)

因此,MainActivity 第 73 行的某些内容为 null,该内容位于 GetRSSDataTaskonPostExecute() 中。

现在我们知道如何找到异常,并且我们知道第 73 行是

itcItems.setAdapter(adapter);

因此您需要查看什么是null以及什么可能导致此异常。如果 itcItems 不是 null,设置断点并查看,那么也许您的 adapternull

现在您知道如何阅读 logcat,这将帮助您仅发布最相关的代码。在这种情况下,其他类和 xml 文件不是必需的,因为异常发生在 MainActivity 中。最初仅发布最相关的代码可以更轻松地在此处获取帮助。

关于java - 解析XML,我没有错误,我不知道出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21104756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com