RuntimeException: native font cannot be created or memory leak for custom TextView download font

There is a HUGE problem in my code where I am loading a font into my folder assets\fonts\from a custom class TextView. The first problem is that it crashes on 4.0 devices with an exception Caused by: java.lang.RuntimeException: native typeface cannot be made. I used the same process here with a method:

public class MyTextView extends TextView {

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

     public MyTextView(Context context, AttributeSet attrs) {
          super(context, attrs);
      }

     public MyTextView(Context context) {
          super(context);
     }


    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(Typeface.createFromAsset(
                    getContext().getAssets(), "fonts/hirakakupronbold.ttf"));
        } else if (style == Typeface.ITALIC) {
            super.setTypeface(Typeface.createFromAsset(
                    getContext().getAssets(), "fonts/hirakakupronitalic.ttf"));
        } else {
            super.setTypeface(Typeface.createFromAsset(
                    getContext().getAssets(), "fonts/hirakakupron.ttf"));
        }
    }
}

, .ttf, , RunTimeException. .otf, 4.0 , , . , , /. , .

+4
4

, , , , TypeFace TextView , TextView. OutOfMemoryException . , TypeFace, , TypeFace, TextView.

TypeFaces:

public class TypeFaces {

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface getTypeFace(Context context, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface typeFace = Typeface.createFromAsset(
                            context.getAssets(), assetPath);
                    cache.put(assetPath, typeFace);
                } catch (Exception e) {
                    Log.e("TypeFaces", "Typeface not loaded.");
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}

TextView:

public class TextViewHirakaku extends TextView {

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

    public TextViewHirakaku(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewHirakaku(Context context) {
        super(context);
    }

    public void setTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(TypeFaces.getTypeFace(getContext(),
                    "fonts/hirakakupronbold.ttf"));
        } else if (style == Typeface.ITALIC) {
            super.setTypeface(TypeFaces.getTypeFace(getContext(),
                    "fonts/hirakakupronitalic.ttf"));
        } else {
            super.setTypeface(TypeFaces.getTypeFace(getContext(),
                    "fonts/hirakakupron.ttf"));
        }
    }
}

, getTypeFace TypeFaces.

+8

Android Studio, , res.

, . my_font.ttf

+7

xml, ::

public class MyTextView extends TextView {

  public MyTextView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init();
  }

 public MyTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
      init();
  }

 public MyTextView(Context context) {
      super(context);
      init();
 }


public void init() {
    Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/hirakakupronbold.ttf");
    setTypeface(tf);

}

}

. , TextView . , "TextView" "com.yourpackage.MyTextView"

,

+1

In my case, the XML namespace prefix that I used for my user view (costum) was not set correctly:

xmlns:costum="http://schemas.android.com/apk/tools"

All I had to do was change it to

xmlns:costum="http://schemas.android.com/apk/res-auto"

& he worked.

0
source

All Articles