2013-03-08 49 views
4

我有我的android活动的自定义字体。Android的自定义字体列表视图

MainActivity.class

private void initControls() { 
    // TODO Auto-generated method stub 
    header = (TextView) findViewById (R.id.tvAccommodations); 
    lv = (ListView) findViewById (R.id.lvAccommodations); 
    text = (TextView) findViewById (R.id.textView); 

    Typeface tf = Typeface.createFromAsset(getAssets(), 
      "fonts/heartbre.ttf"); 
    header.setTypeface(tf); 
    text.setTypeface(tf); 



    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
        R.array.abra_hotel, R.layout.custom_list_text); 
      lv.setAdapter(adapter); 
      header.setText(value); 

custom_list_text.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/list_text" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:textSize="18sp" 
android:textStyle="bold" 
android:text="@string/app_name" 
android:paddingLeft="6dip" /> 

的Android抛出NullPointerException异常。为什么这样?任何帮助表示赞赏。谢谢。

logcat的:

03-08 19:48:03.859: E/AndroidRuntime(413): Caused by: java.lang.NullPointerException 
    03-08 19:48:03.859: E/AndroidRuntime(413): at com.say.philippineexplorer.PlaceAccommodations.initControls(PlaceAccommodations.java:34) 
    03-08 19:48:03.859: E/AndroidRuntime(413): at com.say.philippineexplorer.PlaceAccommodations.onCreate(PlaceAccommodations.java:22) 
+0

你可以把日志 – DjHacktorReborn 2013-03-08 11:56:15

+0

发布日志异常 – Sri 2013-03-08 11:57:30

+0

@DjHacktorReborn我更新了我的文章 – 2013-03-08 12:06:31

回答

10

这里是自定义适配器类和构造

class CustomAdapter extends ArrayAdapter<CharSequence>{ 

    Context context; 
    int layoutResourceId;  
    CharSequence data[] = null; 
    Typeface tf; 

public CustomAdapter(Context context, int layoutResourceId, CharSequence[] data, String FONT) { 
    super(context, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.data = data; 
    tf = Typeface.createFromAsset(context.getAssets(), FONT); 
} 

把你想要在你的资产文件夹中使用,并填写您的列表视图像这样的字体:

listAdapter = new CustomAdapter(this, R.layout.custom_list_text, R.array.abra_hotel, "name_of_font.ttf"); 
+0

它不适用于我的适配器,这里是SO:http:/ /stackoverflow.com/questions/21439707/typeface-not-taking-place-in-custom-arrayadapter?answertab=active#tab-top – vlio20 2014-01-29 19:02:18

+1

您创建了一个名为'tf'的属性并为其设置了一个'Typeface',但是' tf'没有分配给任何东西。这段代码如何设置项目渲染器的字体? – 2014-12-16 21:56:05

+0

我把字体罪“src/assets/fonts/myfont.ttf”并传递到适配器“fonts/myfont.ttf”,它的工作。谢谢! – 2015-11-04 17:26:21