Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AsyncTask onPreExecute未被不确定地调用_Android_Android Asynctask - Fatal编程技术网

Android AsyncTask onPreExecute未被不确定地调用

Android AsyncTask onPreExecute未被不确定地调用,android,android-asynctask,Android,Android Asynctask,我有一个AsyncTask,当它通过Internet上传一些东西时,它应该显示一个进度条。有时它像一个符咒一样工作,有时它不显示任何进度条。代码如下: public class Upload extends AsyncTask<Void, Void, Void> { private ProgressDialog dialog = new ProgressDialog(Activity.this); protected void onPreExecute() {

我有一个AsyncTask,当它通过Internet上传一些东西时,它应该显示一个进度条。有时它像一个符咒一样工作,有时它不显示任何进度条。代码如下:

public class Upload extends AsyncTask<Void, Void, Void> {

    private ProgressDialog dialog = new ProgressDialog(Activity.this);

    protected void onPreExecute() {
        dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
    }
    @Override
    protected Void doInBackground(Void... params) {
        //upload stuff
        return null;
    }

    protected void onPostExecute(Void result) {

        try {
            if (dialog.isShowing())
                dialog.dismiss();
            dialog = null;
        } catch (Exception e) {
            // nothing
        }
        Intent next = new Intent(getApplicationContext(), SecondActivity.class);
        startActivity(next);
        }
    }
}
公共类上载扩展异步任务{
private ProgressDialog=新建ProgressDialog(Activity.this);
受保护的void onPreExecute(){
dialog=ProgressDialog.show(Activity.this,“wait…”,true,true);
}
@凌驾
受保护的Void doInBackground(Void…参数){
//上传资料
返回null;
}
受保护的void onPostExecute(void结果){
试一试{
if(dialog.isShowing())
dialog.dismise();
dialog=null;
}捕获(例外e){
//没什么
}
Intent next=新的Intent(getApplicationContext(),SecondActivity.class);
星触觉(next);
}
}
}

doInBackground和onPostExecute总是有效的,有时它就像一个符咒。但有时,上传时没有进度条。这是比赛条件吗?我不这么认为,但我找不到任何解释。

您在类中创建了两次对象。
ProgressDialog.show
已经返回了一个创建的
ProgressDialog
对象,但是您首先在顶部实例化了它。
ProgressDialog
应该实例化一次,因此请尝试删除顶部的实例化,然后重试,如下所示:

private ProgressDialog dialog;

protected void onPreExecute() {
    dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
}

可能是因为void参数导致了这个问题。请尝试使用整数作为参数:

public class Upload extends AsyncTask<Integer, Integer, Integer>
公共类上载扩展异步任务

您的
AsyncTask
是您的
活动的一个内部类
还是一个单独的类?它是一个内部类。实现like[会导致onRetainonConfiguration实例()中出现空指针我遇到了同样的问题。你找到解决方案了吗?@Jerecthesis在你的日志中查找泄漏的活动异常,尽量不要引用异步任务中的任何内容。除此之外,进度条现在对我来说很好,除了在Galaxy S3s上。我至今还没有了解原因,也许我永远也不会知道。