2010-12-02 1 views
3

표준 클래스에서 getResources() 함수를 호출하는 데 문제가 있습니다. 함수를 사용하려면 모든 가져 오기가 있어야합니다. 수업을 연장하기 위해 필요한 특별한 수업이 있습니까?getResources가 작동하지 않습니다/정의되지 않음 Java

즉시 도움을 주셔서 감사합니다.

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


} 

답변

25

getResoucesContext하는 방법이다. 그래서 당신은 당신의 DbAdapter 생성자에 컨텍스트를 전달하고 그것에서 getResources를 호출 할 수

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

getApplicationContext를 사용하여 DbAdapter 생성자로 보낼 응용 프로그램 컨텍스트를 가져옵니다. – Ben

0

Context's 객체를 정의하고 context 개체의 도움으로 모든 R.<Methods>를 호출합니다.

예 : 코드 위

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

나를 위해 노력하고 있습니다.