I'm trying to set the same action bar (with a custom font) to all my activities, but my "method" works only for the MainActivity called LoginActivity.
public class LoginActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar, null);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Billabong.ttf");
TextView title = ((TextView)v.findViewById(R.id.custom_bar));
title.setText(this.getTitle());
title.setTypeface(font);
this.getActionBar().setCustomView(v);
If I call the same piece of code to another activity, for example MessagesActivity:
public class MessagesActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messages);
this.getActionBar().setDisplayShowCustomEnabled(true);
this.getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.actionbar, null);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Billabong.ttf");
TextView title = ((TextView)v.findViewById(R.id.custom_bar));
title.setText(this.getTitle());
title.setTypeface(font);
this.getActionBar().setCustomView(v);
When I click on the button to open this activity I get the error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rram.wehere/com.rram.wehere.MessagesActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2045)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
at android.widget.TabHost.setCurrentTab(TabHost.java:413)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:548)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18439)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5149)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.rram.wehere.BaseActivity.onCreate(BaseActivity.java:23)
at com.rram.wehere.MessagesActivity.onCreate(MessagesActivity.java:20)
This tells me that this.getActionBar() returns null, even if I already declared that the theme has an ActionBar, as defined in my Manifest and later in my styles.xml like this
<resources>
<style name="myTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:actionBarStyle">@style/myTheme.ActionBar</item>
<item name="android:actionMenuTextColor">@color/actionBarText</item>
</style>
<style name="myTheme.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/bluefb</item>
<item name="android:titleTextStyle">@style/myTheme.ActionBar.Text</item>
<item name="android:gravity">center</item>
</style>
<style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/black</item>
<item name="android:textStyle">bold</item>
</style>
I don't know how to fix this problem, can someone help me to fix this issue?