AsyncTask and Looper.prepare() error

梦想与她 提交于 2019-11-26 13:04:30

Long story:

AsyncTask internally uses a 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. This only works for threads that have a Looper prepared, though.

For more information see http://developer.android.com/reference/android/os/Handler.html

Short story:

Simply wrap every call to FinderMain$1.gotLocation or the creation of AsyncTask within it in a Runnable, and post it to a Handler bound to the UI thread, like this:

class GetLastLocation extends TimerTask {
    private Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    public void run() {
       // ...
       mHandler.post(new Runnable() {
          public void run() {
              locationResult.gotLocation(null);
          }
       });
       // ...
     }
}

I tried this...It worked,hope it will help you..

protected class Asyctast extends AsyncTask<String, Integer, Integer>
{

    @Override
    protected Integer doInBackground(String... params) {
        // TODO Auto-generated method stub


        Log.d("Asynctask", ""+params);  
Looper.prepare();   

         ImageThumbnailsActivity m = new ImageThumbnailsActivity();

            Toast.makeText(ImageThumbnailsActivity.this,""+params ,Toast.LENGTH_SHORT).show();
            final Dialog dialog_options = new Dialog(ImageThumbnailsActivity.this);
            dialog_options.setContentView(R.layout.option);
            dialog_options.show();
        Looper.loop();
        return null;
    }       
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!