我已经设置了一个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;
}
SetWallpaperAsync:
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();
}
}
答案 0 :(得分:1)
替换
new SetWallpaperAsync(this).execute(mImageUrl);
有了这个,
new SetWallpaperAsync(getActivity()).execute(mImageUrl);
答案 1 :(得分:1)
看起来像ImageDetailFragment
不能用作Activity / Context。如果这是一些自定义库,您可能会发现一些方法,如getActivity()
或getContext()
new SetWallpaperAsync(this.getActivity()).execute(mImageUrl); //or this.getContext()