简体   繁体   中英

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 .

您的类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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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