2016-09-30 4 views
0

Ich habe eine benutzerdefinierte Textansicht Klasse für bestimmte Schriftart.Rendering-Fehler für benutzerdefinierte TextView

Meine Klasse: `

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); 
} 

}

mein Layout:

<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" /> 

und Fehler:` Rendering Probleme Die folgenden Klassen können nicht instanziiert werden: - io.pika. pike_store.utils.LobsterTextView (Offene Klasse, Ausnahme anzeigen, Cache löschen) Tipp: Verwenden Sie View.isInEditMode() in Ihren benutzerdefinierten Ansichten, um Code oder s zu überspringen wie Beispieldaten, wenn in der IDE-Ausnahme Details java.lang.NullPointerException bei android.graphics.Typeface.createAssetUid (Typeface.java:219) bei android.graphics.Typeface.createFromAsset (Typeface.java:193) bei io.pika gezeigt. pike_store.utils.LobsterTextView. (LobsterTextView.java:22) bei java.lang.reflect.Constructor.newInstance (Constructor.java:423) bei android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:727) bei android.view. LayoutInflater.rInflate_Original (LayoutInflater.java:858) bei android.view.LayoutInflater_Delegate.rInfllate (LayoutInflater_Delegate.java:70) bei android.view.LayoutInflater.rInfllate (LayoutInflater.java:834) bei android.view.LayoutInflater.rInfllateChildren (LayoutInflater .java: 821) bei android.view.LayoutInflater.rInfllate_Original (LayoutInflater.java:861) bei android.view.LayoutInflater_Delegate.rInfllate (LayoutInflater_Delegate.java:70) bei android.view.LayoutInflater.rInfllate (LayoutInflater.java:834) bei andro id.view.LayoutInflater.rInfllateChildren (LayoutInflater.java:821) bei android.view.LayoutInFlater_Delegate.parseInclude (LayoutInflater_Delegate.java:197) bei android.view.LayoutInflater.parseInclude (LayoutInflater.java:902) bei android.view.LayoutInflater .rInfllate_Original (LayoutInflater.java:854) bei android.view.LayoutInflater_Delegate.rInfllate (LayoutInflater_Delegate.java:70) bei android.view.LayoutInflater.rInfllate (LayoutInflater.java:834) bei android.view.LayoutInflater.rInfllateChildren (LayoutInflater. java: 821) bei android.view.LayoutInflater.inflate (LayoutInflater.java:518) bei android.view.LayoutInflater.inflate (LayoutInflater.java:397) Stapel in die Zwischenablage kopieren Die benutzerdefinierte Ansicht LobsterTextView wurde vorletzter bearbeitet als die letzte Build: Erstellen Sie das Projekt.

Mein Code einmal ausgeführt funktioniert gut.aber dieser Fehler immer im Vordergrund kommen, wenn ich zu Layout-Datei komme.

Wie kann ich das Problem lösen? `

Antwort

0

public class PahariTextView erweitert 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

@ pahari, danke für die Antwort pahari. immer noch das gleiche Problem, und es gibt keinen Unterschied zwischen Ihrem Code und meinem Code. –

+0

Bitte beschreiben Sie im Detail –

+0

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

Verwandte Themen