Android:类中没有活动的警报对话框

发布于 2024-11-29 16:26:21 字数 1759 浏览 0 评论 0原文

我有这个类:

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);     
        }
    }
}

}

和主要活动:

public class ImageFromWeb extends Activity {

private String path = "http://....";
private ImageView imageView;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main);

    ImageView mChart = (ImageView) findViewById(R.id.imview);

    mChart.setTag(path);
    new DownloadImageTask(mChart).execute(path);
}

}

我想在箭头点(在 DownloadImageTask 类中)放置一个警报对话框!我该怎么做?因为这堂课不是一个活动。

谢谢 :)

i have this class:

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);     
        }
    }
}

}

and the main activity:

public class ImageFromWeb extends Activity {

private String path = "http://....";
private ImageView imageView;

public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.main);

    ImageView mChart = (ImageView) findViewById(R.id.imview);

    mChart.setTag(path);
    new DownloadImageTask(mChart).execute(path);
}

}

I want to put in the point of arrow(in DownloadImageTask class) an alert dialog! How can i do this? Because this class isn't an activity.

thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

久夏青 2024-12-06 16:26:21

更改构造函数并传递 Context 对象

Context mContext;
public DownloadImageTask(ImageView imageView,Context mContext) {
    imageViewReference = new WeakReference<ImageView>(imageView);
    this.mContext = mContext;
}

现在您可以使用此 Context 创建对话框

您甚至可以强制转换 mContext到您的 Activity 类并在您的 Activity 中调用函数

change the constructor and pass a Context object

Context mContext;
public DownloadImageTask(ImageView imageView,Context mContext) {
    imageViewReference = new WeakReference<ImageView>(imageView);
    this.mContext = mContext;
}

Now you can use this Context to create dialogs

You can even cast mContext to your Activity class and call functions within your Activity

原来是傀儡 2024-12-06 16:26:21

将异步任务移至您的活动并使用它来调用您的 DownloadImageTask 类和方法。这将使您的生活变得更加轻松。

Move the Async Task to your activity and use that to call your DownloadImageTask class & methods. This will make your life a lot easier.

梦里寻她 2024-12-06 16:26:21

将 Activity 实例传递给要显示对话框的类,然后检查

if(!actvity.isFinishing){
//show dialog
}

pass a Activity instance to the class where you want to display dialog, and check

if(!actvity.isFinishing){
//show dialog
}
﹏雨一样淡蓝的深情 2024-12-06 16:26:21

您可以在应用程序中拥有一个静态上下文,如下所示:

public static Context CurrentContext;

以及一个自定义抽象活动,该活动在创建时设置 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();

这样,无论您处于后台任务还是处于后台任务,您都不必担心上下文直接在 Activity 中它适用于大多数情况。

希望这有帮助!

You can have a static Context in your Application like this:

public static Context CurrentContext;

and a custom abstract Activity that sets currentContext upon creation like this:

public abstract class CustomActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.CurrentContext = this;
    }
}

Then you would get context like 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();

This way you never have to worry about context wether you are in a background task or directly in an Activity it will work for most cases.

hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文