我正在嘗試更改應用程序標籤的字體(我將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" />
這是做還是我失去了一些東西以正確的方式?可以提供一些建議嗎?
謝謝你的時間。
一對夫婦如果(_var_ == NULL工程)檢查不會損害您的代碼,您可能會引用錯誤的佈局常量或者可能找不到您的字體。 – Machinarius 2013-02-10 06:47:08
@Machinarius:這是我相信的TextView,因爲當我嘗試對它進行mAppName.setText()時,文本中沒有任何變化。在此之後,我不確定從sherlock庫訪問textView的正確方法是什麼?或者這是不同的嗎?你有什麼建議 – 2013-02-10 06:52:17
呃,我自己幾乎沒有使用sherlock,但我不認爲這是源於你對sherlock庫的使用。你確定這個textview實際上是你期望的嗎?另外,請記住,在調用setContentView之後,您應該使用findViewById和friends **,以防萬一。 – Machinarius 2013-02-10 06:59:29