在asynctask中取消进度对话框时Android应用程序强制关闭

在asynctask中取消进度对话框时Android应用程序强制关闭,android,android-asynctask,progressdialog,Android,Android Asynctask,Progressdialog,我在主活动中使用一个子类来执行异步任务。我计划以后将其移动到一个单独的文件中,我只是更喜欢这样做,以确认它是否正常工作。由于等待时间可能较长,我正在尝试使用进度对话框执行HTTPRequest。我在doInBackground()中什么也不做,因为我读到你不能从那里访问UI层。最后,我在onPreExecute()上显示我的进度对话框,并在onPostExecute()中关闭它。我分别调用上面的每个任务,这可能是我99%的问题,但我需要能够将我的活动传递给任务,以便它们正常工作 我把很多台阶都摔

我在主活动中使用一个子类来执行异步任务。我计划以后将其移动到一个单独的文件中,我只是更喜欢这样做,以确认它是否正常工作。由于等待时间可能较长,我正在尝试使用进度对话框执行
HTTPRequest
。我在
doInBackground()
中什么也不做,因为我读到你不能从那里访问UI层。最后,我在
onPreExecute()
上显示我的进度对话框,并在
onPostExecute()
中关闭它。我分别调用上面的每个任务,这可能是我99%的问题,但我需要能够将我的活动传递给任务,以便它们正常工作

我把很多台阶都摔成了碎片。我得到了一个空指针异常,这使我相信我的问题来自于不使用execute(),但我似乎不知道它是如何工作的

new CodeRetrievalItem().onPreExecute(MyActivity.this);

new CodeRetrievalItem().onPostExecute(MyActivity.this);

class CodeRetrievalItem extends AsyncTask<Void, Void, Void>{

ProgressDialog dialog;
protected void onPreExecute(Activity actpass){
        dialog = new  ProgressDialog(actpass);
    dialog.setMessage("Loading");
    dialog.setIndeterminate(false); 
    dialog.setCancelable(false);
    dialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
    return null;
}

protected void onPostExecute(Activity actpass){

            // Execute HTTP Request
    try{
        dialog.dismiss();
    }
    catch(Exception e){
        Toast.makeText(actpass, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    }

}

}
new CodeRetrievalItem().onPreExecute(MyActivity.this);
新的CodeRetrievalItem().onPostExecute(MyActivity.this);
类CodeRetrievalItem扩展了AsyncTask{
进程对话;
受保护的void onPreExecute(活动actpass){
dialog=新进度对话框(actpass);
设置消息(“加载”);
dialog.setUndeterminate(false);
对话框。可设置可取消(false);
dialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
返回null;
}
受保护的void onPostExecute(活动actpass){
//执行HTTP请求
试一试{
dialog.dismise();
}
捕获(例外e){
Toast.makeText(actpass,“IOE响应”+e.toString(),Toast.LENGTH\u LONG.show();
}
}
}

像这样更改代码

new CodeRetrievalItem(MyActivity.this).execute();



class CodeRetrievalItem extends AsyncTask<Void, Void, Void>{
Activity context;
public CodeRetrievalItem(Activity actpass)
{
context=actpass;
}
ProgressDialog dialog;
protected void onPreExecute(Activity actpass){
        dialog = new  ProgressDialog(context);
    dialog.setMessage("Loading");
    dialog.setIndeterminate(false); 
    dialog.setCancelable(false);
    dialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
    return null;
}

protected void onPostExecute(Activity actpass){

            // Execute HTTP Request
    try{
        dialog.dismiss();
    }
    catch(Exception e){
        Toast.makeText(context, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    }
newcodererievalItem(MyActivity.this).execute();
类CodeRetrievalItem扩展了AsyncTask{
活动语境;
公共代码检索项目(活动actpass)
{
上下文=actpass;
}
进程对话;
受保护的void onPreExecute(活动actpass){
dialog=新建进度对话框(上下文);
设置消息(“加载”);
dialog.setUndeterminate(false);
对话框。可设置可取消(false);
dialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
返回null;
}
受保护的void onPostExecute(活动actpass){
//执行HTTP请求
试一试{
dialog.dismise();
}
捕获(例外e){
Toast.makeText(上下文,“IOE响应”+e.toString(),Toast.LENGTH_LONG.show();
}

像这样更改代码

new CodeRetrievalItem(MyActivity.this).execute();



class CodeRetrievalItem extends AsyncTask<Void, Void, Void>{
Activity context;
public CodeRetrievalItem(Activity actpass)
{
context=actpass;
}
ProgressDialog dialog;
protected void onPreExecute(Activity actpass){
        dialog = new  ProgressDialog(context);
    dialog.setMessage("Loading");
    dialog.setIndeterminate(false); 
    dialog.setCancelable(false);
    dialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
    return null;
}

protected void onPostExecute(Activity actpass){

            // Execute HTTP Request
    try{
        dialog.dismiss();
    }
    catch(Exception e){
        Toast.makeText(context, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    }
newcodererievalItem(MyActivity.this).execute();
类CodeRetrievalItem扩展了AsyncTask{
活动语境;
公共代码检索项目(活动actpass)
{
上下文=actpass;
}
进程对话;
受保护的void onPreExecute(活动actpass){
dialog=新建进度对话框(上下文);
设置消息(“加载”);
dialog.setUndeterminate(false);
对话框。可设置可取消(false);
dialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
返回null;
}
受保护的void onPostExecute(活动actpass){
//执行HTTP请求
试一试{
dialog.dismise();
}
捕获(例外e){
Toast.makeText(上下文,“IOE响应”+e.toString(),Toast.LENGTH_LONG.show();
}

您使用的
AsyncTask
完全错误。代码中有几个主要错误

异步任务保护方法

不要自己给他们打电话。在中,您可以找到:

不要手动调用onPreExecute()、onPostExecute(结果)、doInBackground(参数…)和onProgressUpdate(进度…)

您必须通过调用
execute(Params…
方法启动
AsyncTask
。这将导致:

  • onPreExecute()
    在后台执行开始之前调用,并在主线程上调用
  • doInBackground(Params…)
  • 后台执行完成后,在主线程上调用
    onPostExecute(Result)
  • 除非调用
    execute(Params…
    否则不会发生任何情况

    通用参数

    此外,您的泛型参数也有问题。
    AsyncTask
    有三个泛型参数
    AsyncTask
    (在您的情况下,
    AsyncTask
    ,您必须在方法的参数中匹配这些参数

    因此,创建:

  • onPreExecute()
    而不是
    onPreExecute(活动actpass)
  • onPostExecute(Void)
    而不是
    onPostExecute(Activity actpass)
  • 您定义的那些方法没有以任何方式绑定到异步执行,它们只是类中的一些额外方法


    如果需要访问
    AsyncTask
    中的活动,请以不同的方式(例如在构造函数中)传递它。请注意,将
    活动
    传递给
    AsyncTask
    可能会导致内存泄漏(最好在此使用
    WeakReference
    并访问
    onPostExecute(结果)中的对话框)
    如果处理不当,可能会导致崩溃。

    您使用的
    AsyncTask
    完全错误。代码中有几个主要错误

    异步任务保护方法

    不要自己给他们打电话。在中,您可以找到:

    不要手动调用onPreExecute()、onPostExecute(结果)、doInBackground(参数…)和onProgressUpdate(进度…)

    您必须通过调用
    execute(Params…
    方法启动
    AsyncTask
    。这将导致:

  • onPreExecute()
    在后台执行开始之前调用,并在主线程上调用
  • doInBackground(Params…)
  • 后台执行后