无法在未调用looper.prepare()的线程内创建处理程序

时间:2013-03-20 11:04:54

标签: android android-asynctask

我有一个Activity,我通过Async任务从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);
    }

}

}

现在这个工作正常,但问题是当这个Async任务调用Default处理程序类时它给了我上面的错误。 在默认的处理程序类中,我创建了一个类的对象,我从中获取了一些数字。 我正在使用这些数字来解析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);
        }

}}

我无法理解发生了什么。 在此先感谢

2 个答案:

答案 0 :(得分:0)

AsyncTask内部使用HandlerHandler基本上允许您从处理程序所分配的线程上的另一个线程发布Runnables,在AsyncTask的情况下始终是调用它的线程。 最简单的方法是在void onCreate()中为您的处理程序创建对象,而不是在AsyncTask

内创建对象

答案 1 :(得分:0)

试试这个。这可能会解决您的问题

private HandlingXMLReading doingWork;

@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);
    doingWork = new HandlingXMLReading(); // created object of default handler class

    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();

            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);
    }

}