Android:类中没有活动的警报对话框
我有这门课:Android:类中没有活动的警报对话框,android,android-activity,android-alertdialog,Android,Android Activity,Android Alertdialog,我有这门课: public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { ImageFromWeb ifw; private String url; private final WeakReference<ImageView> imageViewReference; public DownloadImageTask(ImageView imageView) { imageVi
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageFromWeb ifw;
private String url;
private final WeakReference<ImageView> imageViewReference;
public DownloadImageTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
url = params[0];
try {
return BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Bitmap result) {
if (isCancelled()) {
result = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(result);
}
}
}
@Override
protected void onPreExecute() {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
---------> imageView.setImageResource(R.drawable.pw);
}
}
}
}
我想在箭头点(在DownloadImageTask类中)中放置一个警报对话框!我该怎么做?因为这门课不是一项活动
谢谢:)更改构造函数并传递
上下文
对象
Context mContext;
public DownloadImageTask(ImageView imageView,Context mContext) {
imageViewReference = new WeakReference<ImageView>(imageView);
this.mContext = mContext;
}
Context-mContext;
公共下载ImageTask(ImageView、上下文mContext){
imageViewReference=新的WeakReference(imageView);
this.mContext=mContext;
}
现在,您可以使用此上下文创建对话框
您甚至可以将
mContext
强制转换为活动类并调用活动中的函数更改构造函数并传递上下文
对象
Context mContext;
public DownloadImageTask(ImageView imageView,Context mContext) {
imageViewReference = new WeakReference<ImageView>(imageView);
this.mContext = mContext;
}
Context-mContext;
公共下载ImageTask(ImageView、上下文mContext){
imageViewReference=新的WeakReference(imageView);
this.mContext=mContext;
}
现在,您可以使用此上下文创建对话框
您甚至可以将
mContext
强制转换为活动类,并调用活动中的函数将异步任务移动到活动中,并使用该任务调用下载的ImageTask类和方法。这将使您的生活更加轻松。将异步任务移动到您的活动中,并使用它调用您的DownloadImageTask类和方法。这将使您的生活更加轻松。将活动实例传递给要显示对话框的类,然后检查
if(!actvity.isFinishing){
//show dialog
}
将活动实例传递给要显示对话框的类,并选中
if(!actvity.isFinishing){
//show dialog
}
您的应用程序中可以有如下静态上下文:
public static Context CurrentContext;
public abstract class CustomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.CurrentContext = this;
}
}
AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
dlgBuilder.setTitle("Context Example");
dlgBuilder.setMessage("I am being shown from the application Static context!");
dlgBuilder.setNeutralButton("Ok", null);
dlgBuilder.show();
以及一个自定义抽象活动,该活动在创建时设置currentContext,如下所示:
public static Context CurrentContext;
public abstract class CustomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.CurrentContext = this;
}
}
AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
dlgBuilder.setTitle("Context Example");
dlgBuilder.setMessage("I am being shown from the application Static context!");
dlgBuilder.setNeutralButton("Ok", null);
dlgBuilder.show();
然后您将获得如下上下文:
public static Context CurrentContext;
public abstract class CustomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.CurrentContext = this;
}
}
AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
dlgBuilder.setTitle("Context Example");
dlgBuilder.setMessage("I am being shown from the application Static context!");
dlgBuilder.setNeutralButton("Ok", null);
dlgBuilder.show();
这样,无论您是在后台任务中还是直接在活动中,您都不必担心上下文。在大多数情况下,它都会起作用
希望这有帮助 应用程序中可以有如下静态上下文:
public static Context CurrentContext;
public abstract class CustomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.CurrentContext = this;
}
}
AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
dlgBuilder.setTitle("Context Example");
dlgBuilder.setMessage("I am being shown from the application Static context!");
dlgBuilder.setNeutralButton("Ok", null);
dlgBuilder.show();
以及一个自定义抽象活动,该活动在创建时设置currentContext,如下所示:
public static Context CurrentContext;
public abstract class CustomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.CurrentContext = this;
}
}
AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
dlgBuilder.setTitle("Context Example");
dlgBuilder.setMessage("I am being shown from the application Static context!");
dlgBuilder.setNeutralButton("Ok", null);
dlgBuilder.show();
然后您将获得如下上下文:
public static Context CurrentContext;
public abstract class CustomActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.CurrentContext = this;
}
}
AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(MyApplication.CurrentContext);
dlgBuilder.setTitle("Context Example");
dlgBuilder.setMessage("I am being shown from the application Static context!");
dlgBuilder.setNeutralButton("Ok", null);
dlgBuilder.show();
这样,无论您是在后台任务中还是直接在活动中,您都不必担心上下文。在大多数情况下,它都会起作用
希望这有帮助 我这样做了,在“new DownloadImageTask(mChart).execute(path);”行中,我把这个“new DownloadImageTask(mChart,this.getApplicationContext()).execute(path);”!是这样吗?我在这方面有个错误![update]我在onCreate方法中加入了“Context mContext=this;”,在“new DownloadImageTask(mChart,mContext).execute(path);”之后,它工作了!谢谢!我这样做了,在“new DownloadImageTask(mChart).execute(path);”行中,我把这个“new DownloadImageTask(mChart,this.getApplicationContext()).execute(path);”!是这样吗?我在这方面有个错误![update]我在onCreate方法中加入了“Context mContext=this;”,在“new DownloadImageTask(mChart,mContext).execute(path);”之后,它工作了!谢谢!