2015-08-16 63 views
-1

林getAssets()试图从加载custome字体 “资产\字体” 文件夹获取字体与getResources()从资产文件夹返回NullPointerException异常

这是我的 “TypefaceSingelton” 类代码:

public class TypefaceSingelton { 

     private static TypefaceSingelton instance = new TypefaceSingelton(); 

     private TypefaceSingelton() {} 

     public static TypefaceSingelton getInstance() { 
      return instance; 
     } 
     public static Typeface getFont() { 
      return Typeface.createFromAsset(AppContext.getAppContext(). 
      getResources().getAssets(), "fonts/Myfont.otf"); 
     } 
    } 

和我的 “AppContext” 类:

public class AppContext extends Application { 

    public static Context context; 
    @Override 
    public void onCreate(){ 

     super.onCreate(); 
     AppContext.context = getApplicationContext(); 
    } 
    public static Context getAppContext() { 
     return AppContext.context; 
    } 

}

,我该如何使用它我的MainActivity内:

public class MainActivity extends Activity { 

    private final Typeface FontTF = TypefaceSingelton.getFont(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final AutoCompleteTextView CtextView = (AutoCompleteTextView) findViewById(R.id.autoComplete); 
     CtextView.setTypeface(FontTF); 

,并在 “Typeface.createFromAsset” 里面TypefaceSingelton给出了异常:

08-16 07:55:51.834: E/AndroidRuntime(3109):  Caused by: java.lang.NullPointerException 
08-16 07:55:51.834: E/AndroidRuntime(3109):  at com.test.TypefaceSingelton.getFont(TypefaceSingelton.java:20) 
08-16 07:55:51.834: E/AndroidRuntime(3109):  at com.test.MainActivity.onCreate(MainActivity.java:133) 
08-16 07:55:51.834: E/AndroidRuntime(3109):  at android.app.Activity.performCreate(Activity.java:5104) 
08-16 07:55:51.834: E/AndroidRuntime(3109):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 

即时通讯将使用 “TypefaceSingelton”,以避免性能除了

private final Typeface FontTF; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    Typeface FontTF = TypefaceSingelton.getFont(); 
+3

me =“{packege} .AppContext”...> ...' –

回答

5

你试过了吗?

public static Typeface getFont() { 
      return Typeface.createFromAsset(AppContext.getAppContext().getAssets(), 
      "fonts/Myfont.otf"); 
     } 
0

注意otf字体在Android不支持。只是TTF字库

为什么不使用自定义类TypefacedTextView其延长AppCompatTextView和附加型脸财产

public class TypefacedTextView extends AppCompatTextView { 

public TypefacedTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypefacedTextView); 
    String fontName = styledAttrs.getString(R.styleable.TypefacedTextView_typeface); 
    setTypeFace(fontName); 

    styledAttrs.recycle(); 
} 

public void setTypeFace(String fontName){ 
    try{ 
     Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/" + fontName); 
     setTypeface(typeface); 

    }catch (Exception e){ 
    } 
} 

创建设置样式而像这样

<declare-styleable name="TypefacedTextView"> 
    <attr name="typeface" format="string" /> 
</declare-styleable> 

一个设置样式您在添加此设置样式如果应用程序名称是AppContext,请检查您的清单文件:`

相关问题