为了实现自定义字体,我在这里看到了几个不同的例子,我把自定义字体放在一个抽象类中,这个抽象类在整个应用程序中都使用,下面是我的代码
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
public static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
}
尝试在这里调试我得到了空指针异常
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
对如何解决这一问题提出了一些建议。
最佳答案
在oncreate活动方法中创建typeface对象
public abstract class X extends Activity implements OnClickListener {
private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf ;
public static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
tf = Typeface.createFromAsset(getAssets(), fontPath);
}
关于android - 如何在Android应用程序中使用自定义字体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22878302/