-1

i am using following class for custom font for my textview. import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView;

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

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                               "DIEHLD_DECO.ttf");
        setTypeface(tf);
    }

}

this the xml file containing my textView

<com.example.application1.views.MyTextView 
        android:textSize="27.0sp" 
        android:textColor="@android:color/white" 
        android:gravity="top" 
        android:layout_gravity="center" 
        android:id="@+id/tname" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        />

while running the app my logcat shows following error.

10-31 16:09:33.249: E/AndroidRuntime(17017): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application1/com.example.application1.MySlideShows}: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.application1.views.MyTextView
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.os.Looper.loop(Looper.java:153)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.ActivityThread.main(ActivityThread.java:5086)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at java.lang.reflect.Method.invokeNative(Native Method)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at java.lang.reflect.Method.invoke(Method.java:511)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at dalvik.system.NativeStart.main(Native Method)
10-31 16:09:33.249: E/AndroidRuntime(17017): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.application1.views.MyTextView
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:258)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.Activity.setContentView(Activity.java:1867)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at com.example.application1.MySlideShows.onCreate(MySlideShows.java:73)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.Activity.performCreate(Activity.java:5020)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
10-31 16:09:33.249: E/AndroidRuntime(17017):    ... 11 more
10-31 16:09:33.249: E/AndroidRuntime(17017): Caused by: java.lang.reflect.InvocationTargetException
10-31 16:09:33.249: E/AndroidRuntime(17017):    at java.lang.reflect.Constructor.constructNative(Native Method)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
10-31 16:09:33.249: E/AndroidRuntime(17017):    ... 22 more
10-31 16:09:33.249: E/AndroidRuntime(17017): Caused by: java.lang.RuntimeException: native typeface cannot be made
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.graphics.Typeface.<init>(Typeface.java:175)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at android.graphics.Typeface.createFromAsset(Typeface.java:149)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at com.example.application1.views.MyTextView.init(MyTextView.java:26)
10-31 16:09:33.249: E/AndroidRuntime(17017):    at com.example.application1.views.MyTextView.<init>(MyTextView.java:17)
10-31 16:09:33.249: E/AndroidRuntime(17017):    ... 25 more
mystack
  • 192
  • 9
  • 1
    possible duplicate of [RuntimeException: native typeface cannot be made or memory leak for custom TextView loading font](http://stackoverflow.com/questions/20942671/runtimeexception-native-typeface-cannot-be-made-or-memory-leak-for-custom-textv) – M D Oct 31 '14 at 11:52
  • have you directly put your custom font file in assert or put on fonts folder inside assert ? – Haresh Chhelana Oct 31 '14 at 11:53
  • i have put custom font file in assests folder – mystack Oct 31 '14 at 11:56
  • please check my ans problem due to context reference. – Haresh Chhelana Oct 31 '14 at 11:58

1 Answers1

0

Try this way,hope this will help you to solve your problem.

You have required to your current context reference instead of getContext() so just pass your context reference to intit method from all constructor.

public class MyTextView extends TextView{


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

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

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

    private void init(Context context) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(),"DIEHLD_DECO.ttf");
        setTypeface(tf);
    }

}
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67