2

我正在嘗試更改應用程序標籤的字體(我將ActionBarSherlock用作應用程序中的庫項目)並獲取錯誤。使用ActionBarsherlock時更改應用程序標籤字體

以下是代碼段,我嘗試在setContentView()之後將資產中存儲的外部字體設置爲應用標籤。

mAppName = (TextView) findViewById(R.id.abs__action_bar_title); 
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf"); 
mAppName.setTypeface(face); 

以下是來自logcat的錯誤。該錯誤似乎顯示在mAppName.setTypeface(face)行上。

E/AndroidRuntime(18762): FATAL EXCEPTION: main 
E/AndroidRuntime(18762): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cheats/com.cheats.LandingPage}: java.lang.NullPointerException 
E/AndroidRuntime(18762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
E/AndroidRuntime(18762): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
E/AndroidRuntime(18762): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
E/AndroidRuntime(18762): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
E/AndroidRuntime(18762): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(18762): at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime(18762): at android.app.ActivityThread.main(ActivityThread.java:5039) 
E/AndroidRuntime(18762): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(18762): at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime(18762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
E/AndroidRuntime(18762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
E/AndroidRuntime(18762): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(18762): Caused by: java.lang.NullPointerException 
E/AndroidRuntime(18762): at com.cheats.LandingPage.onCreate(LandingPage.java:89) 
E/AndroidRuntime(18762): at android.app.Activity.performCreate(Activity.java:5104) 
E/AndroidRuntime(18762): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
E/AndroidRuntime(18762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 

我發現文本視圖的以下聲明爲abs__action_bar_title_item.xml應用標籤:

<TextView android:id="@+id/abs__action_bar_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:singleLine="true" 
       android:ellipsize="end" /> 

這是做還是我失去了一些東西以正確的方式?可以提供一些建議嗎?

謝謝你的時間。

+0

一對夫婦如果(_var_ == NULL工程)檢查不會損害您的代碼,您可能會引用錯誤的佈局常量或者可能找不到您的字體。 – Machinarius 2013-02-10 06:47:08

+0

@Machinarius:這是我相信的TextView,因爲當我嘗試對它進行mAppName.setText()時,文本中沒有任何變化。在此之後,我不確定從sherlock庫訪問textView的正確方法是什麼?或者這是不同的嗎?你有什麼建議 – 2013-02-10 06:52:17

+0

呃,我自己幾乎沒有使用sherlock,但我不認爲這是源於你對sherlock庫的使用。你確定這個textview實際上是你期望的嗎?另外,請記住,在調用setContentView之後,您應該使用findViewById和friends **,以防萬一。 – Machinarius 2013-02-10 06:59:29

回答

16

經過大量的試驗我能得到在下面的方式TextView的的參考,現在的字體已經改變:

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); 
if(titleId == 0) 
    titleId = com.actionbarsherlock.R.id.abs__action_bar_title; 

mAppName = (TextView) findViewById(titleId); 
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/handsean.ttf"); 
mAppName.setTypeface(face); 
+0

這很好,而且正是我所需要的。 – Leo 2013-06-26 23:41:59

0

在Xamarin.Android(MonoDroid的 - C#):

如果您在TextView上獲得null時遇到問題,可能是因爲Android版本,上面的代碼在Android 4.2中爲我工作,但沒有在3.1上。

這爲我工作:

TextView title = FindViewById<TextView>(Resources.GetIdentifier("action_bar_title", "id", "android")); 
if (title == null) title = FindViewById<TextView>(Resource.Id.abs__action_bar_title); 

if(title != null) 
{ 
    Android.Graphics.Typeface face = Android.Graphics.Typeface.CreateFromAsset(Assets, "fonts/font.otf"); 
    title.SetTypeface(face, Android.Graphics.TypefaceStyle.Bold); 
} 
0

由阿布舍克Sabbarwal提供的解決方案工作正常(< 3.x和> = 4.x的),但是,在一個情況下,蜂窩,它無法找到的TextView ,正如charlesbock指出的那樣。

調試後,似乎這條線給出了一個積極的titleId,但它與TextView不匹配。

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); 

但是,使用這條線,TextView的成功發現

titleId = R.id.abs__action_bar_title; 

因此,這裏是我的代碼,對任何版本

int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); 
if(titleId == 0 || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)) 
{ 
    titleId = R.id.abs__action_bar_title; 
} 

final TextView appName = (TextView) findViewById(titleId);