2016-05-06 11 views
0

Ich weiß nicht warum, aber meine Klasse kann nicht in XML instanziiert werden. Unten ist mein Code. Ich habe alles versucht, aber nichts scheint zu funktionieren. Versucht, den Cache zu entwerten und neu zu starten, nichts funktioniert.Klasse kann nicht in XML instanziiert werden

CustomFontTextView.java

public class CustomFontTextView extends TextView { 

    public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android"; 



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

     applyCustomFont(context, null); 
    } 

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

     applyCustomFont(context, attrs); 
    } 

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

     applyCustomFont(context, attrs); 
    } 

    private void applyCustomFont(Context context, AttributeSet attrs) { 
     TypedArray attributeArray = context.obtainStyledAttributes(
       attrs, 
       R.styleable.CustomFontTextView); 

     String fontName = attributeArray.getString(R.styleable.CustomFontTextView_font); 
     int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL); 

     Typeface customFont = selectTypeface(context, fontName, textStyle); 
     setTypeface(customFont); 

     attributeArray.recycle(); 
    } 

    private Typeface selectTypeface(Context context, String fontName, int textStyle) { 
     if (fontName.contentEquals(context.getString(R.string.font_name_fontawesome))) { 
      return FontCache.getTypeface("fontawesome.ttf", context); 
     } else if (fontName.contentEquals(context.getString(R.string.font_name_source_sans_pro))) { 
      /* 
      information about the TextView textStyle: 
      http://developer.android.com/reference/android/R.styleable.html#TextView_textStyle 
      */ 
      switch (textStyle) { 
       case Typeface.BOLD: // bold 
        return FontCache.getTypeface("SourceSansPro-Bold.ttf", context); 

       case Typeface.ITALIC: // italic 
        return FontCache.getTypeface("SourceSansPro-Italic.ttf", context); 

       case Typeface.BOLD_ITALIC: // bold italic 
        return FontCache.getTypeface("SourceSansPro-BoldItalic.ttf", context); 

       case Typeface.NORMAL: // regular 
       default: 
        return FontCache.getTypeface("SourceSansPro-Regular.ttf", context); 
      } 
     } else { 
      // no matching font found 
      // return null so Android just uses the standard font (Roboto) 
      return null; 
     } 
    } 
} 

activity.main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="12dp" 
     android:text="Reenath" 
     android:textSize="18sp"/> 

    <com.trupik.virtualmall.activity.customfonts.CustomFontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="12dp" 
     android:text="Reenath" 
     android:textSize="18sp" 
     app:font="@string/font_name_source_sans_pro"/> 

    <com.trupik.virtualmall.activity.customfonts.CustomFontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="12dp" 
     android:text="Reenath" 
     android:textSize="18sp" 
     android:textStyle="bold" 
     app:font="@string/font_name_source_sans_pro"/> 

    <com.trupik.virtualmall.activity.customfonts.CustomFontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="12dp" 
     android:text="Reenath" 
     android:textSize="18sp" 
     android:textStyle="italic" 
     app:font="@string/font_name_fontawesome"/> 

    <com.trupik.virtualmall.activity.customfonts.CustomFontTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="12dp" 
     android:text="&#xf135;&#xf135;&#xf135;&#xf135;&#xf135;" 
     android:textSize="18sp" 
     app:font="@string/font_name_fontawesome"/> 
    </LinearLayout> 

Exception

java.lang.NumberFormatException: For input string: "italic" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Long.parseLong(Long.java:441) 
    at com.android.internal.util.XmlUtils_Delegate.convertValueToInt(XmlUtils_Delegate.java:72) 
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:93) 
    at android.util.BridgeXmlPullAttributes.getAttributeIntValue(BridgeXmlPullAttributes.java:134) 
    at com.android.layoutlib.bridge.android.BridgeXmlBlockParser.getAttributeIntValue(BridgeXmlBlockParser.java:436) 
    at com.trupik.virtualmall.activity.customfonts.CustomFontTextView.applyCustomFont(CustomFontTextView.java:41) 
    at com.trupik.virtualmall.activity.customfonts.CustomFontTextView.<init>(CustomFontTextView.java:26) 
    at sun.reflect.GeneratedConstructorAccessor481.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:458) 
    at org.jetbrains.android.uipreview.ViewLoader.loadClass(ViewLoader.java:158) 
    at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:103) 
    at com.android.tools.idea.rendering.LayoutlibCallbackImpl.loadView(LayoutlibCallbackImpl.java:168) 
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:247) 
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:171) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835) 
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:811) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394) 
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:223) 
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:426) 
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350) 
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:510) 
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:498) 
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:888) 
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:498) 
    at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:72) 
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:610) 
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:607) 
    at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:362) 
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:607) 
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:629) 
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:652) 
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:80) 
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:594) 
    at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:152) 
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:452) 
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:402) 
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54) 
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:137) 
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:589) 
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320) 
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310) 
    at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254) 
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269) 
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227) 
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217) 
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238) 
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:351) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 
+0

Post logcat bitte – Rehan

+0

haben Sie einen Blick auf meine Logcat – rakesh

Antwort

0

schwer zu sagen, was das Problem ist, ohne Ihre logcat

Wie auch immer, wenn Sie nur benutzerdefinierte Schriftarten implementieren möchten, würde ich vorschlagen Calligraphy

ist es ziemlich einfach und unkompliziert.

Hoffe, dass es

+0

ich es in XML implementieren wollen, so habe ich auf diese Weise bin Auswahl – rakesh

+0

Bitte einen Blick auf meine logcat – rakesh

+0

mit Bibliothek können Sie es in XML implementieren in ein besserer Weg – Max

0

hilft die Frage aus der folgenden Zeile in xml angehoben wird:

android:textStyle="italic" 

So höchstwahrscheinlich das Problem ist in selectTypeface Verfahren. Überprüfen Sie, ob Sie italic ordnungsgemäß dort behandelt haben. Das Problem sollte gelöst werden, aber wenn das Problem immer noch beenden, kann ich den genauen Grund nicht sagen, wie es theoretisch das Problem nicht verursachen sollte, da es die Anwendung zum Absturz bringt und Sie es dringend beheben müssen ein Workaround dafür sein.
In Ihrer attrs Datei, fügen Sie folgendes Attribut in Ihrem CustomFontTextView styleable:

<attr name="textStyle"> 
    <flag name="normal" value="0" /> 
    <flag name="bold" value="1" /> 
    <flag name="italic" value="2" /> 
</attr> 

Und in Ihrer CustomFontTextView.java Klasse, die folgende Zeile ändern:

int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL); 

zu

int textStyle = attributeArray.getInt(R.styleable.CustomFontTextView_textStyle, Typeface.NORMAL); 

Und Ändern Sie in Ihren Layout-XML-Dateien alle android:textStyle in app:textStyle. Hoffe, die Lösung funktioniert für Sie.

Verwandte Themen