2016-09-30 101 views
0

我有一個自定義文本視圖類爲特定的字體。自定義TextView呈現錯誤

我的類:`

public LobsterTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "lobster1_4.otf"); 
    this.setTypeface(face); 
} 

public LobsterTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    Typeface face=Typeface.createFromAsset(context.getAssets(), "lobster1_4.otf"); 
    this.setTypeface(face); 
} 

}

我的佈局:

<x.y.z.LobsterTextView 
     android:id="@+id/tv_mobile_name" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:textSize="@dimen/font_large3" 
     android:layout_marginTop="@dimen/fab_margin" 
     android:layout_marginLeft="@dimen/fab_margin" 
     android:textColor="@color/colorAccent_3" 
     android:text="Register with Pika" /> 

和錯誤:`渲染問題下面的類不能被實例化: - io.pika。 pike_store.utils.LobsterTextView(打開類,顯示異常,清除緩存) 提示:在自定義視圖中使用View.isInEditMode()可跳過代碼或s如何在IDE中顯示示例數據異常詳細信息java.lang.NullPointerException at android.graphics.Typeface.createAssetUid(Typeface.java:219)at android.graphics.Typeface.createFromAsset(Typeface.java:193)at io.pika。在android.view上的android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)處的java.lang.reflect.Constructor.newInstance(Constructor.java:423)處的pike_store.utils.LobsterTextView。(LobsterTextView.java:22)。在android.view.LayoutInflater.rInflateChildren的android.view.LayoutInflater.rInflate(LayoutInflater.java:834)上的Layout.flate.Default.Description(LayoutInflater.rv) .java:821)在android.view.LayoutInflater.rInflate(LayoutInflater.java:834)在andro android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)android.view.LayoutInflater_Delegate.parseInclude(LayoutInflater_Delegate.java:197)at android.view.LayoutInflater.parseInclude(LayoutInflater.java:902)at android.view.LayoutInflater .rInflate_Original(LayoutInflater.java:854)at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)at android.view.LayoutInflater.rInflateChildren(LayoutInflater。 android.view.LayoutInflater.inflate(LayoutInflater.java:518)android.view.LayoutInflater.inflate(LayoutInflater.java:397)將堆棧複製到剪貼板LobsterTextView自定義視圖最近編輯的時間比最後一次構建:構建項目。

我的代碼一旦運行fine.but總是出現在前臺,當我來到佈局文件。

我該如何解決這個問題呢?`

回答

0

公共類PahariTextView擴展的TextView {

public PahariTextView(Context context) { 
    super(context); 

    applyCustomFont(context); 
} 

public PahariTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    applyCustomFont(context); 
} 

public PahariTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 

    applyCustomFont(context); 
} 

private void applyCustomFont(Context context) { 
    Typeface customFont = FontCache.getTypeface("fonts/quicksand_light.otf", context); 
    setTypeface(customFont); 
} 

}

+0

@帕哈里,感謝您的答覆帕哈里。仍然會出現同樣的問題,並且您的代碼和我的代碼之間沒有區別。 –

+0

請詳細描述 –

+0

private void applyCustomFont(Context context){ Typeface customFont = FontCache.getTypeface(「fonts/quicksand_light.otf」,context); setTypeface(customFont); –