2014-09-01 10 views
4

Я использую специальный шрифт для TextView и EditText в приложении для Android. Я сделал отдельный класс для шрифтов edittext и textview. Я использую шрифт Calibri для моего текста.Ошибка при настройке стиля пользовательского текста в моем приложении

Но после того, как работает мое приложение, она показывает следующее сообщение об ошибке во всех файлах, как -

com.pack.demo.MyFontedTextView не удалось создать экземпляр, где MyFontedTextView является файл класса и com.pack.demo.MyFontedEditText не удалось экземпляр.

Во всех моих макетах, я получаю исключение во время выполнения, как -.

java.lang.RuntimeException: native typeface cannot be made 
at android.graphics.Typeface.<init>(Typeface.java:175) 
at android.graphics.Typeface.createFromAsset(Typeface.java:149) 
at com.pack.demo.MyFontedTextView.<init>(MyFontedTextView.java:22) 

com.pack.demo.MyFontedTextView (MyFontedTextView.java:22) показывает ошибки в Typeface шрифта = Typeface.createFromAsset (контекст .getAssets(), "calibri.otf"); в файле ниже класса.

Вот код для моего fonted EditText -

public class MyFontedEditText extends EditText { 

     public MyFontedEditText(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
      Typeface font = Typeface.createFromAsset(context.getAssets(), 
        "calibri.otf"); 
      this.setTypeface(font); 
     } 

     public MyFontedEditText(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      Typeface font = Typeface.createFromAsset(context.getAssets(), 
        "calibri.otf"); 
      this.setTypeface(font); 
     } 

     public MyFontedEditText(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
      Typeface font = Typeface.createFromAsset(context.getAssets(), 
        "calibri.otf"); 
      this.setTypeface(font); 
     } 
    } 

Точно так же, мой код для fonted TextView -

public class MyFontedTextView extends TextView { 

    public MyFontedTextView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     Typeface font = Typeface.createFromAsset(context.getAssets(), 
       "calibri.otf"); 
     this.setTypeface(font); 
     this.setTypeface(null, Typeface.BOLD); 
    } 

    public MyFontedTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     Typeface font = Typeface.createFromAsset(context.getAssets(), 
       "calibri.otf"); 
     Log.d("1", "abc"); 
     this.setTypeface(font); 
     this.setTypeface(null, Typeface.BOLD); 
    } 

    public MyFontedTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     Typeface font = Typeface.createFromAsset(context.getAssets(), 
       "calibri.otf"); 
     this.setTypeface(font); 
     this.setTypeface(null, Typeface.BOLD); 
    } 
} 
+0

Добавить ваш шрифт в папку с данными? – Sishin

+0

@ Sishin..yes я добавил, но все еще его не работает. – Ashutosh

+0

Я думаю, что это ошибка типографии. как только проверьте имя файла .ttf на имя, указанное в классе. – Sonali8890

ответ

1

Попробуйте этот путь, надеюсь, это поможет вам решить вашу проблему.

public class MyFontedTextView extends TextView { 

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

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

    public MyFontedTextView(Context context) { 
     super(context); 
     this.context=context; 
     init(context); 
    } 

    private void init(Context mContext) { 
     try { 
      Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "calibri.otf"); 
      setTypeface(tf,Typeface.BOLD); 
     } catch (Throwable e) { 
     } 
    } 
} 
+0

Как я могу назвать указанный файл в MainActivity? – Ashutosh

+0

Вы хотите объявить этот Custom TextView в xml или использовать время выполнения? –

+0

спасибо ... ваш код работал на меня. – Ashutosh

Смежные вопросы