[英]Error calling getResources().openRawResource()
I have to read a file called hello.txt
using the following code on java/eclipse/android: 我必须在java / eclipse / android上使用以下代码读取一个名为hello.txt
的文件:
import java.io.InputStream;
public class Tokenirzer {
public String ReadPath () {
InputStream inputStream = getResources().openRawResource(R.raw.hello);
}
}
However I get the following error: 但是我收到以下错误:
The method getResources() is undefined for the type Tokenirzer 对于Tokenirzer类型,未定义方法getResources()
What am I doing wrong? 我究竟做错了什么?
getResources()
is part of Context
. getResources()
是Context
一部分。 You can use it like that in an Activity
because it inherits from Context
. 您可以像在Activity
那样使用它,因为它是从Context
继承的。 You need to pass in your activity's Context to use getResources
. 您需要传递活动的Context才能使用getResources
。
您的类Tokenirzr
不包含方法getResources()
。
I see this question is old, but I faced similar situation and as per my experience there are 2 solutions. 我看到这个问题很老,但是我遇到了类似的情况,根据我的经验,有两种解决方案。 Either the class has to extend Activity() or a reference should be passed from MainActivity() 该类必须扩展Activity()或应从MainActivity()传递引用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.