Methnani Bilel
Methnani Bilel

Reputation: 1385

android R.id class not found at runtime

I m trying to get all ids at runtime. I m using reflection. but i m getting ClassNotFoundException.

try {
    aClass = classLoader.loadClass(getPackageName()+".R.id");       
    Field[] ID_Fields = aClass.getFields();
    int[] resArray = new int[ID_Fields.length];
    for(int i = 0; i < ID_Fields.length; i++) {    
        try {
          resArray[i] = ID_Fields[i].getInt(null);
         } catch (IllegalAccessException e) {
          e.printStackTrace();
         }
    }
} catch (ClassNotFoundException e1) {
        e1.printStackTrace();
}

Upvotes: 1

Views: 533

Answers (1)

Blackbelt
Blackbelt

Reputation: 157487

id is a inner class of R. you should use the dollar symbol $, to access it

classLoader.loadClass(getPackageName()+".R$id");  

you can also the static method Class.forName

aClass  = Class.forName(getPackageName()+".R$id");

From the documentation:

Returns the Class object associated with the class or interface with the given string name.

Upvotes: 4

Related Questions