1

我想在我的应用程序中实现 Roboto 字体,所以按照 stackoverflow 上的指南,我创建了 2 个新视图,一个用于 AutoCompleteTextView,一个用于常规 TextView

当使用带有 AutoCompleteTextView 的布局时,我创建的一切工作正常,如果我添加没有字体的 TextView 它也可以工作,但是如果我为 TextView 指定字体,我的应用程序会抛出以下 Execption:

android.view.InflateException: Binary XML file line #53: Binary XML file line #53: Error inflating class layouttesting.busalarmclock.com.busalarmclocktesting.Views.TypeFaceTextView

我做了一些挖掘并理解了这条线

Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontName);

正在使我的应用程序崩溃,即使我在其他视图中使用相同的确切代码并且效果很好..

完整的文本视图:

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

public TypeFaceTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    //Typeface.createFromAsset doesn't work in the layout editor. Skipping...
    if (isInEditMode()) {
        return;
    }

    TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TypeFaceTextView);
    String fontName = styledAttrs.getString(R.styleable.TypeFaceTextView_typeface);
    styledAttrs.recycle();

    if (fontName != null) {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontName);
        setTypeface(typeface);
    }
}


public TypeFaceTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

完整的 AutoCompleteTextView 代码:

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

public TypeFaceAutoCompleteTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if(isInEditMode())
    {
        return;
    }

    TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.TypeFaceAutoCompleteTextView);
    String fontName = styledAttributes.getString(R.styleable.TypeFaceAutoCompleteTextView_autocomplete_typeface);
    styledAttributes.recycle();

    if(fontName != null)
    {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+fontName);
        setTypeface(typeface);
    }
}

public TypeFaceAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

编辑: 我只是注意到只有当我在它之前放置一个普通的 TextView 时才会发生这种情况......

添加完整的崩溃报告 - 请注意,没有 Caused By..

06-16 22:12:52.876 21453-21453/layouttesting.busalarmclock.com.busalarmclocktesting E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                  Process: layouttesting.busalarmclock.com.busalarmclocktesting, PID: 21453
                                                                                                  Theme: themes:{default=overlay:com.thoughtlesskyle.theme.roam2, iconPack:com.thoughtlesskyle.theme.roam2, fontPkg:com.thoughtlesskyle.theme.roam2, com.android.systemui=overlay:com.thoughtlesskyle.theme.roam2, com.android.systemui.navbar=overlay:com.thoughtlesskyle.theme.roam2}
                                                                                                  android.view.InflateException: Binary XML file line #53: Binary XML file line #53: Error inflating class layouttesting.busalarmclock.com.busalarmclocktesting.Views.TypeFaceTextView
                                                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                                      at layouttesting.busalarmclock.com.busalarmclocktesting.Fragments.SearchLineFragment.onCreateView(SearchLineFragment.java:22)
                                                                                                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                                                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                                                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                                                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
                                                                                                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
                                                                                                      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                                                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
                                                                                                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
                                                                                                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                                                                                                      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:669)
                                                                                                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:736)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
                                                                                                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
                                                                                                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
                                                                                                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
                                                                                                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                                      at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
                                                                                                      at android.view.View.measure(View.java:18799)
                                                                                                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
                                                                                                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
                                                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
                                                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
                                                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
                                                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                                                    at and

编辑编辑 它在进行冷交换后工作了一次,因为它更改了 rez ID 现在它不再工作了..

编辑编辑编辑 当我清理并重新运行它工作得很好,我想这是一个即时运行的错误?

4

0 回答 0