getResources does not work / undefined Java
I have a problem with caling the getResources()
function in an standard class. All imports must be there to use the function. Is there any special class I need to extend my class?
Thanks for the immediate help.开发者_StackOverflow社区
package com.example.helloandroid;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Resources;
import android.content.Intent;
import android.os.Bundle;
//import android.content.res.Resources;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbAdapter {
public DbAdapter() {
Resources res = getResources();//error: The method getResources() is undefined for the type DbAdapter
//also tyed context.getResources()
}
}
getResouces
is a method of a Context.
So you can pass the context to your DbAdapter constructor and call getResources
from it :
public DbAdapter(Context context) {
Resources res = context.getResources();//error: The method getResources() is undefined for the type DbAdapter
//also tied context.getResources()
}
If your Adapter is for example an ArrayAdapter, you can use this:
getContext().getResources();
Define Context's
object and then call all the R.<Methods>
with the help of context
object .
Eg:
Context ctx;
ctx.getResources().getString(R.string.Forgot_message);
Above code is working for me .
精彩评论