安卓的BufferedReader实例不能在最后试图关闭它来解决 [英] Android BufferedReader instance cannot be resolved at finally trying to close it

查看:189
本文介绍了安卓的BufferedReader实例不能在最后试图关闭它来解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经省略了code无关紧要的部分:

I have omitted irrelevant parts of the code:

[...]
    try {
        URL url = new URL(updateUrl);
        BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
    } finally {
        input.close();
    }
[...]

的问题是,在最终input.close()的Eclipse说输入无法解析

The problem is that on the finally "input.close()" Eclipse says that "input cannot be resolved".

我觉得这可能是一个范围的问题,但我已经看到其他人code,它通常具有同样的形式,所以我不知道为什么它不工作在这里。

I think it may be an scope problem, but I have seen code from other guys and it has usually this same form, so I do not know why it is not working here.

任何提示?

感谢很多提前,

推荐答案

这的确是一个范围错误。
你的输入尝试块内声明的,所以它不能被里面看到最后块。外部声明,所以它是可见的两者,你应该就可以了:

It is indeed a scope error.
Your input is declared inside the try block, so it can't be seen inside the finally block. Declare it outside, so that it is visible to both, and you should be fine:

[...]
    BufferedReader input = null;
    try {
        URL url = new URL(updateUrl);
        input = new BufferedReader(new InputStreamReader(url.openStream()));
[...]
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
    } finally {
        if (input != null)
        {
            try {
              input.close();
            }
            catch (IOException exc)
            {
              exc.printStackTrace();
            }
        }
    }
[...]

这篇关于安卓的BufferedReader实例不能在最后试图关闭它来解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆