简体   繁体   English

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

[英]cannot create handler inside thread that has not called looper.prepare()

I have an Activity in which im getting the results from XML via Async task. 我有一个活动,即时通过异步任务从XML获取结果。 This is my code of the activity from which i am getting the prediction 这是我得到预测的活动代码

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

}

}

Now this works fine, but the problem is when this Async task call the Default handler class It gives me the above error. 现在这可以正常工作,但是问题是当此异步任务调用Default处理程序类时,它给了我上面的错误。 In default handler class I made an object of a class from which I am getting some numbers. 在默认处理程序类中,我创建了一个类的对象,从该对象中我可以得到一些数字。 I am using that numbers to parse the XML. 我正在使用这些数字来解析XML。 But when I removed That class object the code works fine. 但是,当我删除该类对象时,代码可以正常工作。 Here is the code of HandleXMLData.class 这是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);
        }

}}

I am not able to understand what is happening. 我无法理解正在发生的事情。 Thanks In advance 提前致谢

AsyncTask internally uses a Handler . AsyncTask内部使用Handler A Handler basically allows you to post Runnables from another thread on the thread the handler was assigned to, which in the case of AsyncTask is always the thread from which it is called. Handler基本上允许您从分配了该处理程序的线程上的另一个线程发布Runnables ,在AsyncTask的情况下,该线程始终是从其调用该线程的线程。 so simplest method is creating object for your handler in void onCreate() ,instead of creating object inside of AsyncTask 最简单的方法是在void onCreate()为处理程序创建对象,而不是在AsyncTask内部创建对象

Try this. 尝试这个。 This might solve your problem 这可能会解决您的问题

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在尚未调用looper.prepare的线程内创建处理程序 - cant create handler inside thread that has not called looper.prepare 无法在未调用Looper.prepare()的线程内创建处理程序 - Can not create handler inside thread that has not called Looper.prepare() AndroidJavaException无法在未在Unity中调用looper.prepare的线程内创建处理程序 - AndroidJavaException cannot create handler inside thread that has not called looper.prepare in Unity 无法从AsyncTask中显示ProgressDialog:无法在未调用Looper.prepare()的线程中创建处理程序 - Cannot show ProgressDialog from AsyncTask : Can't create handler inside thread that has not called Looper.prepare() PhoneGap android:无法在未调用Looper.prepare()的线程内创建处理程序 - PhoneGap android: getting cannot create handler inside thread that has not called Looper.prepare() “ RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序” - “RuntimeException: Cannot create handler inside the thread that has not called Looper.prepare()” 无法在Handler()内未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() inside Handler() Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() Handler或runOnUiThread解决方案“无法在未调用Looper.prepare()的线程内创建处理程序” - Handler or runOnUiThread solution for “Can't create handler inside thread that has not called Looper.prepare() ” Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM