2013-03-08 45 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