我有一个活动,我通过异步任务从 XML 获取结果。这是我从中获得预测的活动代码
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prediction);
Past = (TextView) findViewById(R.id.textView1);
Present = (TextView) findViewById(R.id.textView2);
Future = (TextView) findViewById(R.id.textView3);
new GetPrediction().execute();
}
class GetPrediction extends AsyncTask <Void, String, String> {
ProgressDialog dialog = new ProgressDialog(Prediction.this);
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setMessage("Getting your fortune");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(Void... b) {
// TODO Auto-generated method stub
try
{
URL website = new URL(baseUrl);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
HandlingXMLReading doingWork = new HandlingXMLReading(); // created object of default handler class
xr.setContentHandler(doingWork);
xr.parse(new InputSource(website.openStream()));
past = doingWork.getPastPrediction();
present = doingWork.getPresentPrediction();
future = doingWork.getFuturePrediction();
}
catch( Exception e)
{
past = e.getMessage();
present = e.getMessage();
future = e.getMessage();
}
return past;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
Past.setText(result);
Present.setText(present);
Future.setText(future);
}
}
}
现在这工作正常,但问题是当这个异步任务调用默认处理程序类时它给了我上述错误。在默认处理程序类中,我创建了一个从中获取一些数字的类的对象。我正在使用这些数字来解析 XML。但是当我删除那个类对象时,代码工作正常。这是 HandleXMLData.class 的代码
public class HandlingXMLReading extends DefaultHandler {
// setting up the opbject
XMLDataCollection prediction = new XMLDataCollection();
/*These next 4 lines gives the error of
* cannot create the handler inside thread
*/
SelectionFuture CardNo = new SelectionFuture();
int PastCard = CardNo.pastCardNo;
int PresentCard = CardNo.presentCardNo;
int FutureCard = CardNo.FutureCardNo;
/* Method Containing the Past Prediction*/
public String getPastPrediction()
{
return prediction.Past();
}
/* Method Containing the Present Prediction*/
public String getPresentPrediction()
{
return prediction.Present();
}
/* Method Containing the Past Prediction*/
public String getFuturePrediction()
{
return prediction.Future();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
if (localName.equals("overview_prediction1")){
String past = attributes.getValue("name");
prediction.setPast(past);
}
if (localName.equals("overview_prediction2")){
String present = attributes.getValue("name");
prediction.SetPresent(present);
}
if (localName.equals("overview_prediction3")){
String future = attributes.getValue("name");
prediction.SetFuture(future);
}
}}
我无法理解发生了什么。提前致谢