简体   繁体   English

无法在setText处解析符号

[英]Cannot Resolve Symbol at setText

I want to build a RSS Reader but I am getting a 'cannot resolve symbol' error in my onStart Method at mRssFeed.setText(rssFeed) where it has a problem with the mRssFeed. 我想构建一个RSS阅读器,但在mRssFeed.setText(rssFeed) onStart方法中mRssFeed.setText(rssFeed) “无法解析符号”错误,其中mRssFeed.setText(rssFeed)出现问题。 Here is my whole class: 这是我全班的:

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

Can someone please help me with this? 有人可以帮我吗?

The variable mRSSfeed is declared inside onCreateView so onStart cannot access it. 变量mRSSfeedonCreateView声明,因此onStart无法访问它。

Declare it outside the method inside the Class instead like this 像这样在类内部的方法外声明它

TextView mRssFeed;

Then in onCreateView change the line to 然后在onCreateView中将行更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM