2012-04-28 62 views
0

我創建一個使用佈局編輯器上的main.xml稱爲textView1的TextView。我想用一個自定義字體,所以我的代碼字體集行成的onCreate,但它似乎並沒有recongnize名稱textView1的Android的TextView的onCreate改變

package com.mystraldesign.memorable; 

import android.app.Activity; 
import android.graphics.Typeface; 
import android.os.Bundle; 

public class MemorableActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf"); 
     textView1.setTypeface(type); 
    } 
} 

我敢肯定,我失去了一些東西簡單,但是這是我的第一次編碼Android仍然感覺我的方式。

+0

下面的解決方案是否解決了您的問題? – 2012-04-28 05:37:49

回答

2
TextView textView1= (TextView) findViewById(R.id.text_info); 
Typeface type= Typeface.createFromAsset(getAssets(),"fonts/optima.ttf"); 
textView1.setTypeface(type); 
+0

伊姆蘭·罕,我試過了,但得到一個錯誤text_info不能得到解決,或者不是一個領域,它提供了遷移代碼的修復 – jskrwyk 2012-04-28 05:36:42

+0

確定你的TextView id爲text_info在main.xml中的佈局。如果沒有,那麼最前一頁添加一個TextView在你想要設置自定義字體的main.xml佈局中 – 2012-04-28 05:38:51

+0

正在愚蠢。 text_info應該是textView1。我太不習慣的Java – jskrwyk 2012-04-28 05:45:30

2

請Intialize TextView的在你的代碼...

試試這個: -

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // Font path 
     String fontPath = "fonts/optima.ttf"; 
     // text view label 
     TextView textView1= (TextView) findViewById(R.id.name); 
    // Loading Font Face 
     Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 
    // Applying font 
     textView1.setTypeface(tf); 
    } 
+1

這詹姆斯只需更換text_info通過TextView的ID。 ADK爲你做了很多工作,但它仍然不能神奇地創建你沒有聲明的Java對象。 – Cheezmeister 2012-04-28 05:41:40

+0

我們在這裏解決問題..不是在這裏宣佈所有的代碼..好的!!! – Hulk 2012-04-28 05:46:01