TypeFace - 片段崩溃我的设备上的应用程序

时间:2015-03-25 11:39:17

标签: android android-fragments

我正在尝试在Fragment中使用TextView的自定义字体,但我的应用程序在手机上崩溃(4.4.2),但Android Studio中没有显示任何错误

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/androidnation_b.ttf");
    TextView txt = (TextView) getActivity().findViewById(R.id.textView1);
    txt.setTypeface(font);

    return inflater.inflate(R.layout.activity_fragment, container, false);
}

当我在返回inflater应用程序之前评论这三行时。 我正在寻找一个近一周的解决方案,但没有任何帮助!

这是我的ttf路径:项目 - >的src /主/资产/字体/ androidnation_b.ttf

我在fragment_layout视图中有id(textView1) 任何想法如何解决这个问题都会很棒。

2 个答案:

答案 0 :(得分:2)

这是解决方案吗

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View v=inflater.inflate(R.layout.activity_fragment, container, false);

Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/androidnation_b.ttf");

TextView txt = (TextView) v.findViewById(R.id.textView1);
txt.setTypeface(font);

return v;
}

答案 1 :(得分:0)

尝试这样做:

View view = inflater.inflate(R.layout.activity_fragment, container, false);
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/androidnation_b.ttf");
TextView txt = (TextView) view.findViewById(R.id.textView1);
txt.setTypeface(font);

return view;