下面是我的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.