0

Özel yazı tipi ayarlamaya çalışıyorum ancak her zaman textview'un boş olduğu hatayı alıyorum.Özel yazı tipi çubuğu ile eylem çubuğu başlığı ayarlanamıyor

int titleId = getResources().getIdentifier("action_bar_title", "id", "android"); 
TextView yourTextView = (TextView) findViewById(titleId); 
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/barrett_normal.ttf"); 

alınıyor aşağıdaki hata:

@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()); 
    } 
+0

sizin OnCreate kod ve xml –

+0

burada bir göz atın gösterir: http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title –

+0

@Aditya 'OnCreate()' yönteminde çok fazla kod yazdım –

cevap

1

bu şekilde

SpannableString s = new SpannableString("My Title"); 
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(), 
     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

// Update the action bar title with the TypefaceSpan instance 
ActionBar actionBar = getActionBar(); 
actionBar.setTitle(s); 

Kontrol here için deneyin: Bu benim onCreate() yöntem

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference 

İşte benim kodudur TypefaceSpan

+0

'txtvw' nedir? –

+0

xml'inizdeki metin görünümünün kimliği ... xml'nizi gösterebilir misiniz? –

+0

Ben bu tür ID alıyorum: 'int titleId = getResources(). GetIdentifier (" action_bar_title "," id "," android "); –