1

我正在使用异步任务在菜单项单击时设置墙纸,但它会继续运行并且不会停止。这是我正在使用的代码:

class ImageTask extends AsyncTask<Void, Void, Void> {
    Context c;
    ProgressDialog pd;

    public ImageTask(Context ctx) { this.c=ctx; }

    @Override
    protected void onPreExecute() {
        pd=ProgressDialog.show(c, "Please Wait", "Setting Wallpaper...");
    }

    public void onPostExecute() {
        pd.dismiss();
        Toast.makeText(c, "Wallpaper set successfully", Toast.LENGTH_SHORT).show();
    }

    protected Void doInBackground(Void... params) {
        WallpaperManager wm1=WallpaperManager.getInstance(c);

        try { wm1.setBitmap(ImageFrag1.bmg1); } 
        catch (IOException e) { e.printStackTrace(); }

        return null;
    }
}

在项目选择功能中,我正在这样做:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_wall: {
            /*WallpaperManager wm=WallpaperManager.getInstance(getActivity().getApplicationContext());
            try{
                wm.setBitmap(bmg1);
                Toast.makeText(getActivity().getBaseContext(), "Wallpaper set successfully",Toast.LENGTH_SHORT).show();
            } catch(IOException e) {
                oast.makeText(getActivity().getBaseContext(), "Wallpaper not set successfully",Toast.LENGTH_SHORT).show();
            }*/
            ImageTask it1=new ImageTask(getActivity());
            it1.execute();
        }
    }
    return false;   
}

我哪里错了?谢谢

4

1 回答 1

7

onPostExecute()方法签名不正确,并且没有覆盖超类中的函数AsyncTask。异步任务不是“连续运行”,只是进度对话框永远不会被解雇。

要修复它,请更换

 public void onPostExecute()

 @Override
 protected void onPostExecute(Void result)
于 2013-08-01T06:40:14.127 回答