开发者

Error calling getResources().openRawResource()

I have to read a file called hello.txt using the following code on java/eclipse/android:

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

What am I doing wrong?


getResources() is part of Context. You can use it like that in an Activity because it inherits from Context. You need to pass in your activity's Context to use getResources.


Your class Tokenirzr does not contain the method 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()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜