0

我已经设置了一个 AsyncTask 类来为我在后台做一些事情,这样 UI 在下载图像然后设置为墙纸时不会冻结。

从 onItemCLick 调用我的 AsyncTask 类时,出现错误。

错误状态“构造函数 SetWallpaperAsync(ImageDetailFragment) 未定义”

我对使用 AsyncTask 比较陌生,所以有人会检查我的代码并告诉我哪里出错了,非常感谢,谢谢。

从此类调用(ImageDetailFragment):

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.image_menu, menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.Favoritewallpaper:



    case R.id.Setwallpaper:

        new SetWallpaperAsync(this).execute(mImageUrl); // <-------- Here - "The constructor SetWallpaperAsync(ImageDetailFragment) is undefined"

    }

    return true;
}

设置壁纸异步:

public class SetWallpaperAsync extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
String image_url;
URL mImageUrl;
String myFileUrl1;
Bitmap bmImg = null;

public SetWallpaperAsync(Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub

    super.onPreExecute();

    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Setting Wallpaper...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();

}

@Override
protected String doInBackground(String... args) {
    // TODO Auto-generated method stub

    try {

        mImageUrl = new URL(args[0]);
        // myFileUrl1 = args[0];

        HttpURLConnection conn = (HttpURLConnection) mImageUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bmImg = BitmapFactory.decodeStream(is);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(String args) {
    // TODO Auto-generated method stub
    WallpaperManager wpm = WallpaperManager.getInstance(context);
    try {
        wpm.setBitmap(bmImg);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    pDialog.dismiss();

}

}
4

2 回答 2

1

代替

new SetWallpaperAsync(this).execute(mImageUrl);

有了这个,

new SetWallpaperAsync(getActivity()).execute(mImageUrl);
于 2013-11-09T13:17:17.867 回答
1

看起来ImageDetailFragment不能用作活动/上下文。如果这是一些自定义库,您可能会找到一些方法,例如getActivity()getContext()

new SetWallpaperAsync(this.getActivity()).execute(mImageUrl); //or this.getContext()
于 2013-11-09T13:18:15.503 回答