ProgressDialog.show里面的AsyncTask从停止执行程序我 [英] ProgressDialog.show inside AsyncTask stop my Program from execution

查看:306
本文介绍了ProgressDialog.show里面的AsyncTask从停止执行程序我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code的原型: -

Here's the prototype of my code:-

class something extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        String str = objectofanotherthing.execute("").get();
    }

    class anotherthing extends AsyncTask
    {
        ProgressDialog Dialog;
        protected void onPreExecute()
        {
            Dialog = ProgressDialog.show(context, "Progressing", "Working on it,");
        }

        protected String doInBackground(String... params) 
        {
            ...
        }

        protected void onPostExecute(Integer result)
        { 
            Dialog.dismiss();
        }
    }
}

当我执行我的code,我的程序似乎停止在ProgressDialog.show,没有崩溃没有错误。当我评论这一部分,程序执行罚款。我不知道可以做什么。谁能帮我这个。先谢谢了。

When I execute my code, my program seems to stop at ProgressDialog.show, no crashes no errors. When I commented this part, the program executes fine. I'm not sure what can be done. Can anyone help me with this. Thanks in advance.

推荐答案

如果你调用与 GET的AsyncTask的(),您阻止UI线程,和你的应用程序可以会停止响应。就拿的get()叫唤所以它只是的execute()

If you call an AsyncTask with get(), you block the UI Thread, and your app can appear to stop responding. Take the get() call out so it is just execute().

objectofanotherthing.execute("");

这意味着现在您的执行独立于UI线程中完成的,所以你不能指望你的字符串来保存有用的东西。为了解决这个问题,使AsyncTask的一个嵌套类的活动内(您片段已经做了这个),已 doInBackground()返回一些有用的东西,并在 onPostExecute()关闭该对话框并设置任何你需要设置。

This means that now your execution is done independently of the UI Thread, so you cannot expect your String to hold anything useful. To get around this, make the AsyncTask a nested class inside your Activity (your snippet does this already), have doInBackground() return something useful, and in onPostExecute() dismiss the dialog and set whatever you need to set.

要嵌套类的另一种是通过活动关闭的AsyncTask的或定义的接口,但是如果任务是一个活动,筑巢更容易。

An alternative to the nested class is passing the Activity off to the AsyncTask or defining an interface, but if the Task is for one activity, nesting is easier.

这篇关于ProgressDialog.show里面的AsyncTask从停止执行程序我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆