2012-04-28 66 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