Android 对话框构造函数中有什么

Android 对话框构造函数中有什么,android,android-asynctask,android-dialog,Android,Android Asynctask,Android Dialog,我知道这应该是上下文。 然而,究竟什么是上下文呢。 通常在类中创建对话框时 我是这样做的: final Dialog dialog = new Dialog(this); 但是现在我正在尝试在异步任务中创建一个对话框 因此,我无法执行上述操作,因为AsyncTask显然不是上下文。 AsyncTask本身就是一个类,也就是说它现在不是一个子类 public class popTask extends AsyncTask<Void, Void, String> { Context

我知道这应该是上下文。 然而,究竟什么是上下文呢。 通常在类中创建对话框时 我是这样做的:

final Dialog dialog = new Dialog(this);
但是现在我正在尝试在异步任务中创建一个对话框 因此,我无法执行上述操作,因为AsyncTask显然不是上下文。 AsyncTask本身就是一个类,也就是说它现在不是一个子类

public class popTask extends AsyncTask<Void, Void, String> {

Context con =

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    final Dialog dialog = new Dialog(con);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("New & Hot advertise");

    // set the custom dialog components - text, image and button
    TextView text = (TextView) dialog.findViewById(R.id.text);
    text.setText("Android custom dialog example!");
    ImageView image = (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.yoda);

    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}


@Override
protected String doInBackground(Void... params) {
    // TODO Auto-generated method stub
    return null;
}



}
公共类popTask扩展了AsyncTask{
上下文con=
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
最终对话框=新对话框(con);
setContentView(R.layout.custom);
dialog.setTitle(“新的和热门的广告”);
//设置自定义对话框组件-文本、图像和按钮
TextView text=(TextView)dialog.findViewById(R.id.text);
setText(“Android自定义对话框示例!”);
ImageView image=(ImageView)dialog.findViewById(R.id.image);
setImageResource(R.drawable.yoda);
Button dialogButton=(Button)dialog.findViewById(R.id.dialogButtonOK);
//如果单击按钮,则关闭自定义对话框
dialogButton.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
dialog.dismise();
}
});
dialog.show();
}
@凌驾
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
返回null;
}
}

以下是从执行异步任务的活动发送上下文的两种方法:

popTask pTask = new popTask(mContext); //OR
pTask.execute(mContext);
popTask
中创建一个私有变量,您可以在其中设置上下文

在第一个选项中,您需要为接受上下文的类
popTask
创建一个构造函数

对于第二个选项,如果没有向函数
doInBackground()
传递任何有意义的内容,则可以更改以下行:

public class popTask extends AsyncTask<Object, Void, String>

protected Object doInBackground(Object... params) {
this.mContext = (Context) params[0];
公共类popTask扩展了AsyncTask
受保护对象doInBackground(对象…参数){
this.mContext=(Context)参数[0];
}


您将在
doInBackground()
中接收上下文对象,您可以在
popTask
类的私有上下文变量中设置该对象,然后在
doInBackground()
函数中访问它。

添加到Samir的答案中

修改您的代码以使构造函数接受调用类的上下文

public class popTask extends AsyncTask<Void, Void, String> {

    private Context context;
    public popTask(Context context)
    {
        this.cotext=context;
    }
公共类popTask扩展了AsyncTask{
私人语境;
公共popTask(上下文)
{
cotext=上下文;
}
然后
Dialog=newdialog(上下文);

在调用活动中,按如下方式调用此Asynctask


new popTask(ActivityName.this).execute();

创建第一个选项后。我可以通过执行pTask.execute();是的,我包含了一个OR,表示两个解决方案中的一个,但不能同时使用两个。如果使用第一个选项,则可以使用
pTask.execute()
。希望答案有帮助。似乎接受ActivityName.class,但不接受ActivityName.this。不确定差异。请告诉你。再次感谢帮助。哦,对不起……应该是ActivityName。这..我更新了我的答案。也接受答案,以便对其他有类似问题的人有所帮助。现在没有任何合作伙伴编译错误,但对话框无法显示…也许我需要在某个地方使用runOnUiThread()?