【发布时间】:2016-03-24 07:23:31
【问题描述】:
我正在尝试设置自定义字体,但总是收到 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
-
@Aditya 我在
onCreate()方法中只写了那么多代码
标签: android nullpointerexception android-actionbar textview android-assets