得到错误在我的应用程序设置自定义文本样式 [英] Getting error in setting custom text style in my application

查看:167
本文介绍了得到错误在我的应用程序设置自定义文本样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的TextView和EditText上自定义字体在我的Andr​​oid应用程序。我已经做了fonted的EditText TextView的和一个单独的类。我现在用的是宋体字体为我的文字。

但在运行我的应用程序后,它显示所有文件中的以下错误的 -

com.pack.demo.MyFontedTextView未能实例,其中MyFontedTextView是类文件,并com.pack.demo.MyFontedEditText失败的实例。

在我所有的布局,我收到运行时异常的 -

 了java.lang.RuntimeException:本地字体无法进行
 在android.graphics.Typeface<&初始化GT;(Typeface.java:175)
 在android.graphics.Typeface.createFromAsset(Typeface.java:149)
 在com.pack.demo.MyFontedTextView<&初始化GT;(MyFontedTextView.java:22)

com.pack.demo.MyFontedTextView(MyFontedTextView.java:22)表示误差在字样字体= Typeface.createFromAsset(context.getAssets(),calibri.otf);在下面的类文件。

下面是code为我fonted的EditText -

 公共类MyFontedEditText扩展的EditText {        公共MyFontedEditText(上下文的背景下){
            超级(上下文);
            // TODO自动生成构造函数存根
            字体字型= Typeface.createFromAsset(context.getAssets()
                    calibri.otf);
            this.setTypeface(字体);
        }        公共MyFontedEditText(上下文的背景下,ATTRS的AttributeSet){
            超(背景下,ATTRS);
            字体字型= Typeface.createFromAsset(context.getAssets()
                    calibri.otf);
            this.setTypeface(字体);
        }        公共MyFontedEditText(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
            超(背景下,ATTRS,defStyle);
            字体字型= Typeface.createFromAsset(context.getAssets()
                    calibri.otf);
            this.setTypeface(字体);
        }
    }

同样,我的code为fonted的TextView -

 公共类MyFontedTextView扩展的TextView {    公共MyFontedTextView(上下文的背景下){
        超级(上下文);
        // TODO自动生成构造函数存根
        字体字型= Typeface.createFromAsset(context.getAssets()
                calibri.otf);
        this.setTypeface(字体);
        this.setTypeface(NULL,Typeface.BOLD);
    }    公共MyFontedTextView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        字体字型= Typeface.createFromAsset(context.getAssets()
                calibri.otf);
        Log.d(1,ABC);
        this.setTypeface(字体);
        this.setTypeface(NULL,Typeface.BOLD);
    }    公共MyFontedTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        字体字型= Typeface.createFromAsset(context.getAssets()
                calibri.otf);
        this.setTypeface(字体);
        this.setTypeface(NULL,Typeface.BOLD);
    }
}


解决方案

试试这个办法,希望这将帮助你解决你的问题。

 公共类MyFontedTextView扩展的TextView {    私人上下文的背景下;
    公共MyFontedTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        初始化(上下文);
    }    公共MyFontedTextView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        初始化(上下文);
    }    公共MyFontedTextView(上下文的背景下){
        超级(上下文);
        this.context =背景;
        初始化(上下文);
    }    私人无效的init(上下文mContext){
        尝试{
            字体TF = Typeface.createFromAsset(mContext.getAssets(),calibri.otf);
            setTypeface(TF,Typeface.BOLD);
        }赶上(Throwable的E){
        }
    }
}

I am using a custom font for TextView and EditText in my android app. I have made a seperate class for fonted edittext and textview. I am using the Calibri font for my text.

But after running my app, it is showing following error in all files as -

com.pack.demo.MyFontedTextView failed to instantiate where MyFontedTextView is the class file and com.pack.demo.MyFontedEditText failed to instantiate.

In all my layouts, I am getting the runtime exception as -

  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) shows error in Typeface font = Typeface.createFromAsset(context.getAssets(), "calibri.otf"); in below class file.

Here is the code for my 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);
        }
    }

Similarly, my code for 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);
    }
}

解决方案

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

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

这篇关于得到错误在我的应用程序设置自定义文本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆