-1

assests フォルダーからフォント ファイルを取得するコードは次のとおりです。

public static Typeface getMyFont(Context context, String resource) {
    InputStream is;
    Typeface font = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    AssetManager assetManager = context.getResources().getAssets();

    try {
        is = classLoader.getResourceAsStream(resource);
        is = assetManager.open(resource);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        while ((line = br.readLine()) != null) {
            Log.e("wwwww", line);
        }
        br.close();
        font = Typeface.createFromAsset(context.getAssets(), line);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return font;
}

ここでのリソースは、リンクされている assests フォルダーの font/MYFONT.ttf (ショートカット) ですが、次の行で nullpointer 例外が発生します。

font = Typeface.createFromAsset(context.getAssets(), line);
4

2 に答える 2

1
    my_font = Typeface.createFromAsset(context.getAssets(), "fonts/MYFONT.ttf"); 
于 2013-03-14T14:07:35.600 に答える
1

私も同じ問題に直面していました。フォントを追加する部分をtry&catchブロックで囲んでnullポインター例外をキャッチすることができるので、私の場合は、

Typeface comingsoon = Typeface.createFromAsset(getApplicationContext().getAssets(), "ComingSoon.ttf");

になる

try{
    Typeface comingsoon = Typeface.createFromAsset(getApplicationContext().getAssets(), "ComingSoon.ttf");
}
catch(NullPointerExecption e){
     //...
}
于 2013-07-20T15:41:29.947 に答える