gpt4 book ai didi

java - onPreExecute方法没有被调用

转载 作者:行者123 更新时间:2023-12-02 09:42:24 26 4
gpt4 key购买 nike

我正在尝试显示 Toast当我调用AsyncTask时与 new saveWithStickers(BaseActivity.this).execute(); 一起上课当我运行 onPreExecute() 下面的代码时,通知用户保存过程正在启动由于某种逻辑原因未调用方法,因此 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();
}
}
});

最佳答案

用此替换您的代码

@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到您的家长 Activity

关于java - onPreExecute方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56970990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com