JUAN
JUAN

Reputation: 523

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?

Upvotes: 0

Views: 6237

Answers (3)

Bibaswann Bandyopadhyay
Bibaswann Bandyopadhyay

Reputation: 3547

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()

Upvotes: 0

DeeV
DeeV

Reputation: 36045

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.

Upvotes: 3

Ian Macalinao
Ian Macalinao

Reputation: 1668

Your class Tokenirzr does not contain the method getResources().

Upvotes: 2

Related Questions