字体崩溃(空引用)

Crash with typeface (null reference)

正如您在屏幕上看到的那样,当我在 AVD 上启动 activity 时,我的项目崩溃了。 我的项目在其他布局上使用了正确的字体和 "setTypeFont",但是这个不起作用,我不知道为什么! 我尝试了很多东西,但我现在真的迷路了!! 在 android studio 的 gradle 更新后几天就出现了这个问题。

感谢您的帮助。

JAVA code

Layout

Fonts folder

Gradle

Crash log

确保您没有在其他地方使用过 R.id.titre1。按 Ctrl 键并将鼠标悬停在 id 上并检查它指向哪个布局。

gradle没问题。主要问题是 Android 由于 rootview (View c) changed 而失去对 textView 的访问权限。 事实上,这个错误的发生是因为引擎没有找到任何带有 id t1 的文本。

1- 确保 textview t1 在 fragment_a1

中定义

2- 这样做:

private View v = null ;
private TextView t1 ;
private Typeface mtf1 ;
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (v == null){
            v = inflater.inflate(R.layout.fragment_setting_app_tab2, container, false);
            mtf1 = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Lato-Bold.ttf");
            t1 = (TextView) v.findViewById(R.id.titre1):
            t1.setTypeface (mtf1) ;
        }

        return v;
    }

它对我有用,我希望对你也有用。