我想設置自定義字體,但總是出現textview
爲空的錯誤。這裏是我的代碼:無法設置自定義字體的操作欄標題
int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
TextView yourTextView = (TextView) findViewById(titleId);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/barrett_normal.ttf");
獲得以下錯誤:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference
這是我onCreate()
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowCustomEnabled(true);
int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
tv = (TextView) findViewById(titleId);
tf = Typeface.createFromAsset(getAssets(), "fonts/barrett_normal.ttf");
tv.setTypeface(tf);
} catch (Exception e) {
e.printStackTrace();
}
setActionBarTitle(this.getTitle());
}
顯示您的OnCreate代碼和XML –
到這裏看看:http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title –
@Aditya我在'onCreate()'方法中只寫了很多代碼 –