Android 单任务 + AsyncTask 和意外的变量值更改

发布于 2024-12-02 00:06:52 字数 1675 浏览 1 评论 0原文

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

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

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

https://i.sstatic.net/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);
    }
}

I have a main activity with launch mode set as "singleTask". When I bring it to the foreground and the onNewIntent method is called, it runs an AsyncTask containing a while loop which reads lines from a text file.

Part way into this loop, the value of one of my integer variables changes to 0. This doesn't always happen in the same stage in the loop cycle and the loop has nothing to do with this variable at all so I don't understand why this is happening.

Here's an image that might better explain the problem:

https://i.sstatic.net/ogLQh.jpg

EDIT: Code as requested:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花海 2024-12-09 00:06:52

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

myVariable seems to be changed from UI thread (activity thread). Try to add logging in each place where you modify myVariable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文