如何在列表视图中的非活动类中显示进度对话框

时间:2014-09-06 07:47:31

标签: android android-activity progressdialog

我有Listview的适配器类,它位于一个名为AdapterItems的类中,该类在ArrayAdapter中扩展。

知道如何在此类中使用未在Activity中扩展的进度对话框?

这是我对listview的填充方法

public void fill(final ArrayAdapter<RssParser.Item> adapter, final RssParser.Item item, final int position)
    {

        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {

                ActivityStartup.handler.post(new Runnable() {

                    @Override
                    public void run() {
                        ActivityStartup.dismissLoadingDialog();
                        txtTitle.setText(item.title);
                        txtDate.setText(item.pubDate);
                        txt_time.setText(item.time);
                        txtDescription.setText(item.description);
                        layoutRoot.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {

                                if (progress == null) {
                                    progress = new ProgressDialog(v.getContext());
                                    progress.setTitle("title");
                                    progress.setMessage("loading");
                                }
                                progress.show();
                                G.SelectedApplication = item;

                                Intent intent = new Intent(G.context, Adapter_Description.class)
                                        .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.putExtra("INPUT", item.link + "");
                                intent.putExtra("INPUT_DESC", item.description + "");
                                G.context.startActivity(intent);
                            }
                        });

                    }
                });
            }
        });
        thread.start();

    }
}

当我点击列表视图中的项目以查看对话框时我遇到了崩溃,这是我的日志

  

09-06 12:04:10.368:E / AndroidRuntime(5112):致命异常:主要   09-06 12:04:10.368:E / AndroidRuntime(5112):android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.view.ViewRootImpl.setView(ViewRootImpl.java:571)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.app.Dialog.show(Dialog.java:281)   09-06 12:04:10.368:E / AndroidRuntime(5112):at com.myApp.course.app.feed_reader.AdapterItems $ ViewHolder $ 1 $ 1 $ 1.onClick(AdapterItems.java:71)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.view.View.performClick(View.java:4204)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.view.View $ PerformClick.run(View.java:17355)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.os.Handler.handleCallback(Handler.java:725)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.os.Handler.dispatchMessage(Handler.java:92)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.os.Looper.loop(Looper.java:137)   09-06 12:04:10.368:E / AndroidRuntime(5112):在android.app.ActivityThread.main(ActivityThread.java:5041)   09-06 12:04:10.368:E / AndroidRuntime(5112):at java.lang.reflect.Method.invokeNative(Native Method)   09-06 12:04:10.368:E / AndroidRuntime(5112):at java.lang.reflect.Method.invoke(Method.java:511)   09-06 12:04:10.368:E / AndroidRuntime(5112):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   09-06 12:04:10.368:E / AndroidRuntime(5112):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)   09-06 12:04:10.368:E / AndroidRuntime(5112):at dalvik.system.NativeStart.main(Native Method)   09-06 12:04:36.292:E / InputDispatcher(467):channel'a69034a0 com.myApp.course.app.feed_reader / com.myApp.course.app.feed_reader.ActivityStartup(server)'〜频道无法恢复将被处置!

2 个答案:

答案 0 :(得分:1)

只需将活动Context传递给您的non_activity类的构造函数,然后使用该上下文显示您的ProgressDialog

private Activity activityContext;

Public YourClassName(Activity activityContext)
{
  this.activityContext = activityContext;
}

然后使用此Context初始化ProgressDialog:

progressDialog = new ProgressDialog(activityContext);

答案 1 :(得分:0)

你必须通过

  

活动背景

到您的非活动课程。然后使用该上下文显示Dialog