GetResources not working / undefined Java

I have a problem setting a function getResources()in a standard class. To use this function, all imports must be used. Is there any special class that 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()
    }


}
+3
source share
2 answers

getResoucesis the Context method . Therefore, you can pass the context to your DbAdapter constructor and call from it getResources:

public DbAdapter(Context context) {
     Resources res = context.getResources();//error: The method getResources() is undefined for the type DbAdapter
            //also tied context.getResources()
}
+25
source

Define an object Context's, and then call everything R.<Methods>using the object context.

For instance:

Context ctx;
ctx.getResources().getString(R.string.Forgot_message);

The code is working on me.

0

Source: https://habr.com/ru/post/1778166/


All Articles