isabsent
isabsent

Reputation: 3763

How to access resource file from class which is not extending Activity in Android?

I have a class with context passed in the constructor:

public abstract class AbstractDbAdapter {
    protected Context context; 

    public AbstractDbAdapter(Context context) {
        this.context = context;     
        String email = context.getString(R.string.my_email);
    }

It doesn't compile with the error at last string:

package R does not exist context.getString(R.string.my_email);

Package R definetly exists! I use the same call:

context.getString(R.string.my_email);

in other class which extends Activity without any problems. Moreover, I see strings:

public static final class string {
    ...
    public static final int my_email=0x7f06001e;
    ...
}

in R.java. What should I do to get my code working in the class not extending Activity!?

Upvotes: 0

Views: 1441

Answers (1)

Matthew Rudy
Matthew Rudy

Reputation: 16834

Your activities, and the AbstractDbAdapter must be in different packages.

You just need to import the R class.

import com.example.R;

Upvotes: 2

Related Questions