带有 ProgressDialog 的外部 AsyncTask 类 [更新:并返回

External AsyncTask class with ProgressDialog [Update: and returning back?](带有 ProgressDialog 的外部 AsyncTask 类 [更新:并返回?])

本文介绍了带有 ProgressDialog 的外部 AsyncTask 类 [更新:并返回?]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**更新:(见下文)**我已经环顾了几天,但找不到直接的答案.有人说可以有人说可以完成有人说不能.我对这个越来越疯狂了.

**Updated: (See below)**I have been looking around for couple of days and can't find a straight answer to this. Some say it possible to some say to accomplish some say it's not. I am getting crazy on this.

我想要的只是让 AsyncTaskTask 显示一个外部类的进度条.为此,我正在传递上下文,正如您在主类中看到的那样.但是无论我怎么尝试,我都会得到 NullPointerException.

What I want is just to have the AsyncTaskTask showing a progressbar an external class. To do this I am passing the context as you can see in the main class. But whatever I try I get NullPointerException.

工作代码示例表示赞赏.谢谢

Working code examples is appreciated. Thank you

顺便说一下,使用的是 Android 2.2.

Using Android 2.2 by the way.

主要:

import android.app.Activity;
import android.os.Bundle;

public class AsyncDemo extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new AsyncClass(this).execute();
    }
}

AsyncClass.java

AsyncClass.java

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;

public class AsyncClass extends AsyncTask<Void, String, Void> {
    private Context context;
    ProgressDialog dialog = new ProgressDialog(context);

    public AsyncClass(Context cxt) {
        context = cxt;
    }

    @Override
    protected void onPreExecute() {
        dialog.setTitle("Please wait");
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... unused) {
        SystemClock.sleep(2000);
        return (null);
    }

    @Override
    protected void onPostExecute(Void unused) {
        dialog.dismiss();
    }
}

更新:我有一个后续问题:使用上面的代码,是否可以以某种方式将 onPostExecute 方法的值返回到主类?(对不起,我是个菜鸟)我试过这样的事情:

Update: I have a follow up question: Using the above code, is it possible to return a value from the onPostExecute method back to the main class, somehow? (Sorry about beeing noobish) I have tried something like this:

String result = new AsyncClass(this).execute();

然后是一个返回字符串的方法.但我不能这样做,因为我得到了:

and then a method that return back a string. But I can't do that because i got:

Type mismatch: cannot convert from AsyncTask<String,Void,Void> to String

我能做些什么来解决这个问题?谢谢.

What can I do to tackle this problem? Thanks.

推荐答案

您正在创建具有空上下文的 ProgressDialog.以下代码对我有用.

You were creating the ProgressDialog with a null context. The following code worked for me.

public class AsyncClass extends AsyncTask<Void, String, Void> {
    private Context context;
    ProgressDialog dialog;

        public AsyncClass(Context cxt) {
            context = cxt;
            dialog = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute() {
            dialog.setTitle("Please wait");
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... unused) {
            SystemClock.sleep(2000);
            return (null);
        }

        @Override
        protected void onPostExecute(Void unused) {
            dialog.dismiss();
        }
    }

这篇关于带有 ProgressDialog 的外部 AsyncTask 类 [更新:并返回?]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 ProgressDialog 的外部 AsyncTask 类 [更新:并返回

基础教程推荐