安卓:进度对话框不显示 [英] Android: Progress Dialog Not Displaying

查看:123
本文介绍了安卓:进度对话框不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用以下code启动子活动主要活动:

I have a main activity that launches a child activity using the following code:

Intent intent = new Intent();
intent.setClassName(MyChildActivity.class.getPackage().getName(), MyChildActivity.class.getName());
((Activity)context).startActivity(intent);

我想执行的子活动一个耗时的任务,并想显示ProgressDialog,而我这样做。我的code是这样的:

I am trying to perform a time-consuming task in the child activity and would like to display a ProgressDialog while I do so. My code looks like this:

private ProgressDialog _progressDialog;

private OnClickListener btn_onClick = new OnClickListener() {
    public void onClick(View v) {
        _progressDialog = ProgressDialog.show(
            v.getContext(),
            "Please wait",
            "Performing task..."
        );

        TaskThread t = new ExportThread(v.getContext());
        t.start();
    }
};

private class TaskThread extends Thread{
    private Context _context;

    public TaskThread(Context context) {
        _context = context;
    }

    private Handler _handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            _progressDialog.dismiss();
        }
    };


    @Override
    public void run() {
        performTask(_context);
        _handler.sendEmptyMessage(0);
    }
}

有关某种原因,ProgressDialog不显示。如果我使用的主要活动相同code,它的作品 - 但不是在孩子的活动。此外,以下code也无法显示ProgressDialog(但吐司确实显示):

For some reason, the ProgressDialog is not displaying. If I use that same code in the main activity, it works - but not in the child activity. In addition, the following code also fails to display the ProgressDialog (but the Toast does display):

private ProgressDialog _progressDialog;

private OnClickListener _btn_onClick = new OnClickListener() {
    public void onClick(View v) {
        _progressDialog = ProgressDialog.show(
            v.getContext(),
            "Please wait",
            "Performing task..."
        );

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        _progressDialog.dismiss();

        Toast.makeText(v.getContext(), "Done with progress dialog.", Toast.LENGTH_SHORT).show();
    }
};

任何想法了吗?难道我们不能从一个子活动显示ProgressDialog?

Any ideas out there? Are we not allowed to display a ProgressDialog from a child activity?

感谢您。

推荐答案

为什么要使用线程代替的异步任务的?

why use thread instead of async task?

异步任务实现该方法的 onProgressUpdate publishProgress 这可以很容易地显示和更新UI /进程对话框。

Async task implements the method onProgressUpdate and publishProgress which makes it easy to display and update UI/progress dialogs.

下面是一些例子code:的http://android-projects.de/2010/12/08/threading-in-android-apps-wir-entwickeln-einen-zahler/

Here is some example code: http://android-projects.de/2010/12/08/threading-in-android-apps-wir-entwickeln-einen-zahler/

这篇关于安卓:进度对话框不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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