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

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

}

}
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 に答える