0

これは私のTYPEFACEクラスです

public class TYPEFACE {

    public static final Typeface Rupee(Context ctx){
        Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "Rupee_Foradian.ttf");
        return typeface;
    }
    public static final Typeface ArialRounded(Context ctx){
        Typeface typeface = Typeface.createFromAsset(ctx.getAssets(), "Rupee_Foradian.ttf");
        return typeface;
    }

}

そして、次の方法でリフレクションを使用して、このクラスのメソッドにアクセスしようとします

Method method;
TYPEFACE typeface;
try{            
    method = typeface.getClass().getMethod("Rupee",Context.class);
}catch(Exception e){
    System.out.println("method : "+e);
}

しかし、それはnullpointerexception.

05-21 17:12:07.026: I/System.out(14917): method : java.lang.NullPointerException

この方法の何が問題なのかわかりません。誰か助けてください。

前もって感謝します。

4

3 に答える 3

4

typefaceですnull。初期化されていないためです

これをに変更TYPEFACE typeface = new TYPEFACE();

于 2013-05-21T11:30:29.327 に答える
2

これNullPointerExceptionは、変数の書体が null であるためです。

TYPEFACEただし、そのメソッドを呼び出すためにクラスのインスタンスは必要ありません。試してみてください

try{            
    method = TYPEFACE.class.getMethod("Rupee",Context.class);
    method.invoke(null, new Context()); // since its a static method, can be invoked directly
}catch(Exception e){
    System.out.println("method : "+e);
}
于 2013-05-21T11:40:10.527 に答える
0

書体を初期化してから使用したいので、

あなたの2番目のコードは....のようなものです

Method method;
TYPEFACE typeface=new TYPEFACE();
try{            
    method = typeface.getClass().getMethod("Rupee",Context.class);
}catch(Exception e){
    System.out.println("method : "+e);
}
于 2013-05-21T11:36:32.550 に答える