如何从Android中的泛型类返回AlertDialogs?

时间:2010-11-15 17:01:10

标签: android alertdialog android-context

在一个不扩展任何其他类的公共类中,我正在做一些评估工作,如果需要将某些内容传达给用户,我想返回一个Dialog。

我正在使用AsyncTask从主UI运行此类实例的方法:

private OnlineActivities onlineActivities = new OnlineActivities();

new DoOnlineActivity().execute(getApplicationContext());

private class DoOnlineStuff extends AsyncTask<Context, Integer, Dialog> {
    @Override
    protected Dialog doInBackground(Context... params) {
        return onlineActivities.start(params[0]);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        Log.v(RuntimeVars.getMyName(), "AsyncOnlineTask in progess");
    }

    @Override
    protected void onPostExecute(Dialog result) {
        if (result != null)
            result.show();
    }
}

现在,public Dialog start() OnlineActivities.java方法使用通过其参数分配的Context创建对话框。

当对话框返回onPostExecute时,我收到以下异常

11-14 23:53:43.303: ERROR/AndroidRuntime(1320): FATAL EXCEPTION: main
11-14 23:53:43.303: ERROR/AndroidRuntime(1320): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.view.ViewRoot.setView(ViewRoot.java:509)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.app.Dialog.show(Dialog.java:241)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at info.myProject.messaging.ConversationList$DoOnlineActivity.onPostExecute(ConversationList.java:245)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at info.myProject.messaging.ConversationList$DoOnlineActivity.onPostExecute(ConversationList.java:1)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.AsyncTask.finish(AsyncTask.java:417)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.os.Looper.loop(Looper.java:123)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at java.lang.reflect.Method.invoke(Method.java:521)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-14 23:53:43.303: ERROR/AndroidRuntime(1320):     at dalvik.system.NativeStart.main(Native Method)

我想我不能使用这样的上下文。但是替代方案是什么呢?我如何外包我总是需要做的一些基本的东西,甚至是一些通用的Dialogs?

我是否需要使用自定义意图?

2 个答案:

答案 0 :(得分:1)

不要这样做。将上下文传递给构造函数。然后在你在后台进行操作之前显示活动,然后在之后清除它。

new DoOnlineActivity().execute(getApplicationContext());

private class DoOnlineStuff extends AsyncTask<Void, Integer, Dialog> {

    Context ctx;
    Dialog dialg

    public DoOnlineStuff(Context ctx){

    this.ctx = ctx;

}


    @Override
    protected void onPreExecute(){
    dialg = createdialog;
    dialog.show()
}

    @Override
    protected Dialog doInBackground(Void... void) {
        // Do stuff in background
        publishProgress(0);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        Log.v(RuntimeVars.getMyName(), "AsyncOnlineTask in progess");
    }

    @Override
    protected void onPostExecute(Dialog result) {
        dialog.dismiss()

}

答案 1 :(得分:0)

我得到了一部分工作。问题是使用getApplicationContext()Android SDK

  

返回当前进程的单个全局Application对象的上下文。

我认为这种背景与实际活动的背景不同。

因此,当我使用this代替getApplicationContext()时,会显示对话框:

private OnlineActivities onlineActivities = new OnlineActivities();

new DoOnlineActivity(this).execute();

private class DoOnlineStuff extends AsyncTask<Void, Integer, Dialog> {
    Context ctx;
    Dialog dialog;

    public DoOnlineActivity(Context ctx){
        this.ctx = ctx;
    }

    @Override
    protected Dialog doInBackground(Void... params) {
        return onlineActivities.start(ctx);
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        Log.v(RuntimeVars.getMyName(), "AsyncOnlineTask in progess");
    }

    @Override
    protected void onPostExecute(Dialog result) {
        if (result != null) {
            dialog = result;
            dialog.show();
        }
    }
}

但是:现在我在Dialog中创建的按钮不会做任何事情。我想我的对话框中的OnClickListener()在我的主要用户界面中没有响应:

final View layout = View.inflate(ctx, R.layout.dialog_phonenumber_request, null);
((TextView) layout.findViewById(R.id.label)).setText(ctx.getString(R.string.dialog_insert_self_phonenumber_request_text1));

final EditText savedPhoneNumber = ((EditText) layout.findViewById(R.id.phonenum));
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setIcon(0);
builder.setTitle(ctx.getString(R.string.dialog_insert_self_phonenumber_request_title));

builder.setPositiveButton(ctx.getString(R.string.save),
    new Dialog.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String toSavePhoneNumber = savedPhoneNumber.getText().toString().trim();

            Database thisDBHandler = new Database(ctx);
            thisDBHandler.open();
            thisDBHandler.insertSetting(RuntimeVars.getDBPhoneNumberName(), toSavePhoneNumber);
            thisDBHandler.close();
        }
    }
);
builder.setView(layout);
return builder.create();

所以最后一步应该告诉OnClickListener()使用哪个上下文。但是怎么做呢? new Dialog.OnClickListener()不会对上下文采取任何参数吗?

谢谢!
S上。