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); 
相关问题