无法在setText处解析符号

时间:2017-02-10 14:51:42

标签: android

我想构建一个RSS阅读器,但我在mRssFeed.setText(rssFeed)的onStart方法中遇到了“无法解决符号”错误,因为mRssFeed存在问题。这是我的全班:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.feed_fragment_portrait, container, false);
    TextView mRssFeed = (TextView) rootView.findViewById(R.id.rss_feed);
    return rootView;
}
@Override
public void onStart() {
    super.onStart();
    InputStream in = null;
    try {
        URL url = new URL("http://www.androidpit.com/feed/main.xml");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        in = conn.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        for (int count; (count = in.read(buffer)) != -1; ) {
            out.write(buffer, 0, count);
        }
        byte[] response = out.toByteArray();
        String rssFeed = new String(response, "UTF-8");
        mRssFeed.setText(rssFeed);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

变量mRSSfeedonCreateView内声明,因此onStart无法访问它。

在Class内部的方法之外声明它,而不是像

TextView mRssFeed;

然后在onCreateView中将行更改为

mRssFeed = (TextView) rootView.findViewById(R.id.rss_feed);