getResources does not work / undefined Java

假如想象 提交于 2019-12-19 09:15:34

问题


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.

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


}

回答1:


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



回答2:


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 .



来源:https://stackoverflow.com/questions/4338400/getresources-does-not-work-undefined-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!