android – 在我的应用程序中设置自定义文本样式时出错

android – 在我的应用程序中设置自定义文本样式时出错,第1张

概述我在我的 Android应用程序中使用TextView和EditText的自定义字体.我已经为fonted edittext和textview创建了一个单独的类.我使用Calibri字体作为我的文字. 但在运行我的应用程序后,它在所有文件中显示以下错误 – com.pack.demo.MyFontedTextView无法实例化MyFontedTextView是类文件的位置,并且com.pack.d 我在我的 Android应用程序中使用TextVIEw和EditText的自定义字体.我已经为Fonted 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 Font = Typeface.createFromAsset(context.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(),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,int defStyle) {        super(context,defStyle);        Typeface Font = Typeface.createFromAsset(context.getAssets(),Typeface.BolD);    }}
解决方法 试试这种方式,希望这可以帮助您解决问题.

public class MyFontedTextVIEw extends TextVIEw {    private Context context;    public MyFontedTextVIEw(Context context,defStyle);        init(context);    }    public MyFontedTextVIEw(Context 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) {        }    }}
总结

以上是内存溢出为你收集整理的android – 在我的应用程序中设置自定义文本样式时出错全部内容,希望文章能够帮你解决android – 在我的应用程序中设置自定义文本样式时出错所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1125301.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存