0

In Android there is a undeclared (hidden) class named "android.graphics.FontFamily" and I want to create static array of it. Any thing like this:

Class<?> clazz = Class.forName("android.graphics.FontFamily"); // Ok.
Class<?> clazz_array = Class.forName("android.graphics.FontFamily[]"); // Method threw 'java.lang.ClassNotFoundException' exception.

To build this hypothetical code:

FontFamily[] families = {fontFamily};

How can I do it?

Thankyou.

mh taqia
  • 3,506
  • 1
  • 24
  • 35

2 Answers2

3

You should be able to create an array reflectively then get its class. Example:

Class <?> clazz = Class.forName("android.graphics.FontFamily");
Object fontFamily = clazz.newInstance();
Object families = Array.newInstance(clazz, 1);
Array.set(families, 0, fontFamily);
mh taqia
  • 3,506
  • 1
  • 24
  • 35
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
1

You should specify the fully-qualified name as specified in the Class documentation. So in your case it would be:

Class<?> clazzArray = Class.forName("[Landroid.graphics.FontFamily;");

The [ indicates an array, and then the L prefix and semi-colon suffix are to indicate that the part in the middle is a class name.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194