Android 如何在我的代码中调试由于sax解析而导致的错误
我正在开发一个android应用程序,其中使用SAX解析器进行XML解析。 我的XML当前在我的WAMP服务器上。在我的应用程序中,当我点击一个按钮时,我会看到一个标题列表,当我选择一个标题时,我会看到它的详细信息。 我的问题是我的应用程序正在崩溃。我找了很多,但找不到任何解决办法。 当我从本地文件获取XML数据时,它可以工作,但当我从URL获取数据时,它崩溃了。 我希望你能帮我做点什么 这是我的日志Android 如何在我的代码中调试由于sax解析而导致的错误,android,eclipse,saxparser,Android,Eclipse,Saxparser,我正在开发一个android应用程序,其中使用SAX解析器进行XML解析。 我的XML当前在我的WAMP服务器上。在我的应用程序中,当我点击一个按钮时,我会看到一个标题列表,当我选择一个标题时,我会看到它的详细信息。 我的问题是我的应用程序正在崩溃。我找了很多,但找不到任何解决办法。 当我从本地文件获取XML数据时,它可以工作,但当我从URL获取数据时,它崩溃了。 我希望你能帮我做点什么 这是我的日志 07-13 17:30:14.874: E/AndroidRuntime(6844):
07-13 17:30:14.874: E/AndroidRuntime(6844): FATAL EXCEPTION: main
07-13 17:30:14.874: E/AndroidRuntime(6844): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.svnitupdates/com.example.svnitupdates.UpdatesActivity}: java.lang.NullPointerException
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.os.Looper.loop(Looper.java:137)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-13 17:30:14.874: E/AndroidRuntime(6844): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 17:30:14.874: E/AndroidRuntime(6844): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 17:30:14.874: E/AndroidRuntime(6844): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-13 17:30:14.874: E/AndroidRuntime(6844): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-13 17:30:14.874: E/AndroidRuntime(6844): at dalvik.system.NativeStart.main(Native Method)
07-13 17:30:14.874: E/AndroidRuntime(6844): Caused by: java.lang.NullPointerException
07-13 17:30:14.874: E/AndroidRuntime(6844): at com.example.svnitupdates.UpdatesActivity.onCreate(UpdatesActivity.java:66)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.Activity.performCreate(Activity.java:5104)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-13 17:30:14.874: E/AndroidRuntime(6844): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-13 17:30:14.874: E/AndroidRuntime(6844): ... 11 more
这是我的更新活动
package com.example.svnitupdates;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class UpdatesActivity extends Activity
{
private ListView listview;
NewsList newsList = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.updates);
listview = (ListView) findViewById(R.id.updatesListView);
final ArrayList<String> list = new ArrayList<String>();
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL ("http://localhost/My%20files/data.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler( myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
//xr.parse(new InputSource(is.getByteStream()));
} catch (Exception e) {
System.out.println("XML Parsing Excpetion = " + e);
}
newsList = MyXMLHandler.newsList;
for (int i = 0; i < newsList.getTitle().size(); i++)
{
list.add(newsList.getTitle().get(i));
}
final UpdatesAdapter adapter = new UpdatesAdapter(UpdatesActivity.this , android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setItemsCanFocus(false);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent i = new Intent(UpdatesActivity.this, ArticleActivity.class);
i.putExtra("s", position);
startActivity(i);
}
});
}
}
package com.example.svnitupdates;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ArticleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
Intent i = getIntent();
savedInstanceState = i.getExtras();
int k = savedInstanceState.getInt("s");
updateArticleView(k);
}
public void updateArticleView(int position) {
NewsList newslist = null;
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL ("http://localhost/My%20files/data.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandlerArticle myXMLHandler = new MyXMLHandlerArticle();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Parsing Excpetion = " + e);
}
newslist = MyXMLHandlerArticle.newsList;
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(newslist.getNews().get(position));
}
}
我的XML文件在这里
<maintag>
<item>
<title>1st round interview of Student Council on 2nd April</title>
<news>Student Council interviews on 2nd April. Students are requested to be on time. 1st round of interviews will be held in th Seminar Hall of respective department</news>
</item>
<item>
<title>Interviews for 1st yearites for Drishti on 2nd April</title>
<news>Interviews for drishti are being held on next monday. Those interested be present at sharp 5:00 PM at drishti room</news>
</item>
<item>
<title>Workshop on Basic GUI Development by ACM on 3rd and 4th April</title>
<news>Acm workshop on basic GUI. Those interested register on the link http://bit.ly/1073SMx. Install the required software from the link provided. Bring your laptops along(max 3 people/laptop).</news>
</item>
<item>
<title>Registration for Treasure Hunt by CES starting from 5th April</title>
<news>Registration for treasure hunt starting from 5th april. Prizes worth 15,000/- can be won.Do register and win.</news>
</item>
</maintag>
4月2日学生会第一轮面试
4月2日学生会访谈。学生们被要求准时。第一轮面试将在相关部门的第次研讨会大厅举行
4月2日对Drishti一年级学生的采访
德里什蒂的采访将于下周一举行。感兴趣的人员将于下午5:00准时到达drishti房间
ACM于4月3日和4日举办的基本GUI开发研讨会
关于基本GUI的Acm研讨会。有兴趣的人在链接上注册http://bit.ly/1073SMx. 从提供的链接安装所需的软件。随身携带笔记本电脑(最多3人/台笔记本电脑)。
由4月5日起登记参加CES寻宝活动
从4月5日开始注册寻宝。可赢取价值15000/-的奖品。请注册并赢取。
您在updateActivity
的onCreate()方法的第66行崩溃。这是上面代码列表中的哪一行?对于(inti=0;i package com.example.svnitupdates;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ArticleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article);
Intent i = getIntent();
savedInstanceState = i.getExtras();
int k = savedInstanceState.getInt("s");
updateArticleView(k);
}
public void updateArticleView(int position) {
NewsList newslist = null;
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL ("http://localhost/My%20files/data.xml");
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandlerArticle myXMLHandler = new MyXMLHandlerArticle();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Parsing Excpetion = " + e);
}
newslist = MyXMLHandlerArticle.newsList;
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(newslist.getNews().get(position));
}
}
<maintag>
<item>
<title>1st round interview of Student Council on 2nd April</title>
<news>Student Council interviews on 2nd April. Students are requested to be on time. 1st round of interviews will be held in th Seminar Hall of respective department</news>
</item>
<item>
<title>Interviews for 1st yearites for Drishti on 2nd April</title>
<news>Interviews for drishti are being held on next monday. Those interested be present at sharp 5:00 PM at drishti room</news>
</item>
<item>
<title>Workshop on Basic GUI Development by ACM on 3rd and 4th April</title>
<news>Acm workshop on basic GUI. Those interested register on the link http://bit.ly/1073SMx. Install the required software from the link provided. Bring your laptops along(max 3 people/laptop).</news>
</item>
<item>
<title>Registration for Treasure Hunt by CES starting from 5th April</title>
<news>Registration for treasure hunt starting from 5th april. Prizes worth 15,000/- can be won.Do register and win.</news>
</item>
</maintag>