2

如果我使用 Activity 实例,我可以显示对话框,但是当我使用 Context 或 Application Context 实例时,Dialog 没有显示。

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(title);
            builder.setMessage(msg);

            if (null != positiveLabel) {
                builder.setPositiveButton(positiveLabel, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                        if (null != listener) {
                            listener.onOk();
                        }
                    }
                });
            }

            if (null != negativeLable) {
                builder.setNegativeButton(negativeLable, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                        if (null != listener) {
                            listener.onCancel();
                        }
                    }
                });
            }

            builder.create().show();

你能给我一个不使用 Activity 实例来显示对话框的解决方案吗

4

4 回答 4

26

This is one of the MOST important things that you must always remember about Contexts. There are 2 types of contexts, Activity contexts and Application contexts. You will observe in many UI related classes, a Context is passed. This is not the Application context! In such cases you must always pass an Activity Context. Except for a Toast, no other UI component will work with Application context.

Application Context is always passed when you want some service or component which is Application related, like the Telephony Manager, Location Manager etc.

For UIs, you must always pass a context that is UI related which is the Activity.

于 2012-04-15T03:47:58.743 回答
7

问题是我最近也面临的问题,如果没有活动实例,您将无法创建对话框。getApplicationContext() 调用也不起作用。我这样做的方法是从一个活动调用一个创建对话框的方法,并传递“this”,即对该活动的引用作为参数。

如果您打算重用此代码,作为可重用组件或作为在多个位置创建对话框的机制,请创建一个基本活动类并在其中包含此方法,并根据需要在子类活动中使用它。

于 2012-04-15T03:40:03.747 回答
2

出于某种原因 [至少在 android 2.1 中] toast 可以在应用程序上下文中,但不能在进度对话框中

MyActivity.this 是一个特定于活动的上下文,不会崩溃

MyActivity.getApplicationContext() 是全局的,会导致进度条崩溃,在以后的版本中也会触发。

于 2012-09-15T13:15:06.960 回答
0

要使用上下文,它必须从创建对话框的活动中传递。这只有在您创建自己的对话框而不是使用AlertDialog.BuilderJava 提供的对话框时才有可能。这是一个自定义的,我们将使用它来获取应用程序上下文。

public class CustomDialogPopUp extends Dialog implements View.OnClickListener {

public Activity a;
public Context c;
public Dialog d;
//CUSTOMIZE YOUR DIALOG AS YOU LIKE
RecyclerView pointAbsorber;
ImageButton addPoint, attachPoint;
EditText addContent;

//VERY IMPORTANT CONSTRUCTOR THAT WE SHALL USE TO GET THE CONTEXT
public CustomDialogPopUp(@NonNull Context context, Activity a, Context c) {
    super(context);
    this.a = a;
    this.c = c;
}

//INITIALIZING YOUR CUSTOM DIALOG VIEWS AND WHAT NOT. IT NEEDS AN XML FILE BY THE WAY
public void initViewSnActions(){
    pointAbsorber = findViewById(R.id.pointsRV);
    addPoint = findViewById(R.id.addPoint);
    attachPoint = findViewById(R.id.attachPoint);
    addContent = findViewById(R.id.addContent);
    
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
    initViewSnActions();
}

//ON CLICK LISTENER FOR YOUR VIEWS
@Override
public void onClick(View v) {
    
}

}

要传递应用程序上下文,必须在您希望它出现的任何类中对其进行初始化,如下所示。注意活动上下文是如何传递的

NotesPopUp notesPopUp = new NotesPopUp(PersistentTest.this, getParent(), getApplicationContext());
notesPopUp.show();
于 2020-07-25T14:31:22.153 回答