【问题标题】:onPreExexute method is not called未调用 onPreExecute 方法
【发布时间】:2019-07-10 12:44:34
【问题描述】:

当我使用new saveWithStickers(BaseActivity.this).execute(); 调用AsyncTask 类以通知用户正在启动保存过程时,我试图显示Toast,当我运行onPreExecute() 方法下方的代码以获取一些逻辑reason 没有被调用,所以 Toast 也没有出现,runOnUiThread 是这种行为的原因吗?

@SuppressLint("StaticFieldLeak")
public class saveWithStickers extends AsyncTask<Void, File, File> {
    File fileSaved;
    Context mContext;

    saveWithStickers(Context context) {
        mContext = context;
    }

    @Override
    protected File doInBackground(Void... voids) {
        fileSaved = FileUtil.getNewFile(BaseActivity.this, "VAPOGRAM");
        if (fileSaved != null)
            runOnUiThread(() -> {
                stickerView.save(fileSaved, true);
            });
        return fileSaved;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        content.setVisibility(View.GONE);
        Toast.makeText(mContext, "saving ...", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostExecute(File file) {
        super.onPostExecute(file);
}


saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckPermissionUtil checkPermissionUtil = new CheckPermissionUtil(BaseActivity.this);
                if (Build.VERSION.SDK_INT >= 23) {
                    if (checkPermissionUtil.checkPermission(1812)) {
                        new saveWithStickers(BaseActivity.this).execute();
                    } else
                        checkPermissionUtil.requestPermission(1812);
                } else {
                    new saveWithStickers(BaseActivity.this).execute();
                }
            }
        });

【问题讨论】:

  • 你能把代码 sn-p 贴在你调用 Async 类的地方,并提到活动或服务等类类型吗?
  • @HariNJha 谢谢你的评论你能再看一遍帖子吗
  • 它是否在片段中:尝试在 Toast.makeTet(getContext(), "text", Toast.LENGTH_SHORT).show() 中的 getContext();

标签: java android android-asynctask android-runonuithread


【解决方案1】:

用这个替换你的代码

@SuppressLint("StaticFieldLeak")
        public class saveWithStickers extends AsyncTask<Void, File, File> {
            File fileSaved;
            Context mContext;

            saveWithStickers(Context context) {
                mContext = context;
            }

            @Override
            protected File doInBackground(Void... voids) {
                fileSaved = FileUtil.getNewFile(BaseActivity.this, "PHOTO EDITOR");
                if (fileSaved != null)
                    runOnUiThread(() -> stickerView.save(fileSaved, true));
                return fileSaved;
            }

            @SuppressLint("SetTextI18n")
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Toast.makeText(mContext, "Saving ...", Toast.LENGTH_SHORT).show();
            }

            @Override
            protected void onPostExecute(File file) {
                super.onPostExecute(file);

        }
    }

要访问上下文,您需要将其传递或使用 WeakRerefence 到您的父活动

【讨论】:

  • 非常感谢您的回答,当我使用 new saveWithStickers(getApplicationContext()).execute(); 调用 saveWithStickers 时,什么也没发生,同样的问题仍然存在,我看不到 Toast(正在保存.. ..)
  • 尝试打印一些东西以确保它真的没有被调用?
  • 是的,使用Log 我可以看到onPreExecute 已被调用,但是Toast 没有
  • 一些愚蠢的建议,但我希望它可以帮助你。在 runOnUiThread() 方法中调用 Toast。在super.onPreExecute();之前第二次调用toast方法第三次去掉@SuppressLint("SetTextI18n")注解
  • 试试saveWithStickers(this).execute();不是applicationContext
猜你喜欢
  • 2015-01-09
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 2016-01-18
  • 2014-05-23
  • 2021-02-23
  • 1970-01-01
  • 2014-10-23
相关资源
最近更新 更多