gpt4 book ai didi

java - Android singleTask + AsyncTask 和意外的变量值变化

转载 作者:行者123 更新时间:2023-12-01 05:40:52 24 4
gpt4 key购买 nike

我有一个主要 Activity ,启动模式设置为“singleTask”。当我将其置于前台并调用 onNewIntent 方法时,它会运行一个包含 while 循环的 AsyncTask,该循环从文本文件中读取行。

在进入这个循环的过程中,我的一个整数变量的值更改为 0。这并不总是发生在循环周期的同一阶段,并且循环与该变量完全无关,所以我不这样做不明白为什么会发生这种情况。

这是一张可能更好地解释问题的图片:

/image/ogLQh.jpg

编辑:按要求编写代码:

private class ReadFile extends AsyncTask<String, String, String> implements DialogInterface.OnCancelListener {

protected void onPreExecute() {

//Launch dialog
}

protected String doInBackground(String... path) {

try {

File f = new File(path[0]);
FileInputStream in = new FileInputStream(f);
InputStreamReader ir;
ir = new InputStreamReader(in);

BufferedReader br = new BufferedReader(ir);
StringBuffer sb = new StringBuffer();
String str = new String();

while ((str = br.readLine()) != null) {

Log.i("My Variable",Integer.toString(myVariable));

sb.append(str);
sb.append("\n\n");

}

br.close();
myTextFile = sb.toString();

} catch (IOException e) {
e.printStackTrace();
}

return "";
}

protected void onProgressUpdate(String... progress) {

//Nothing here

}

protected void onPostExecute(final String unusedString) {

//Dismiss dialog

}

protected void onCancelled() {
finish();
}

public void onCancel(DialogInterface dialog) {
cancel(true);
}
}

最佳答案

myVariable 似乎是从 UI 线程( Activity 线程)更改的。尝试在修改 myVariable 的每个位置添加日志记录。

关于java - Android singleTask + AsyncTask 和意外的变量值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7225740/

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