java - 在fragment android中设置自定义字体时出现异常错误

标签 java android android-fragments

这是代码,它基本上是一个简单的菜单驱动应用程序,我尝试在 fragment 内使用自定义字体,这导致了问题。 我已经在一个 Activity 中成功使用了这种字体,该菜单 Activity 启动了另一个 Activity ,在该 Activity 中该 fragment 被夸大了。 我不确定这是否是导致问题的原因,所以请帮助我。

package com.cbs.CoronaTheCarnival;

    import android.app.Fragment;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.LayoutInflater;
    import android.view.ViewGroup;
    import android.widget.TextView;


    /**
     * Created by aditya on 2/4/14.
     */
    public class AboutFragment extends Fragment{

        View rootView;
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            View rootView = inflater.inflate(R.layout.fragment_about, container, false);
            initFonts();
            return rootView;

        }

         public void initFonts(){

            TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);
            Typeface changeFont = Typeface.createFromAsset(getActivity().getAssets(), "TRACK.OTF");
            tv_title.setTypeface(changeFont);
            TextView tv_theme = (TextView) rootView.findViewById(R.id.tv_about_theme);
            tv_theme.setTypeface(changeFont);

        }


    }

这是 logcat,它说异常错误,我不知道为什么它不起作用,请帮助我,它杀死了我,凌晨 4:21。

02-06 04:19:41.534    3591-3591/com.cbs.CoronaTheCarnival E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cbs.CoronaTheCarnival/com.cbs.CoronaTheCarnival.HomeActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.cbs.CoronaTheCarnival.AboutFragment.initFonts(AboutFragment.java:27)
            at com.cbs.CoronaTheCarnival.AboutFragment.onCreateView(AboutFragment.java:20)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.Activity.performStart(Activity.java:5113)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

最佳答案

您永远不会为成员rootView分配值。改变

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.fragment_about, container, false);
    initFonts();
    return rootView;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    rootView = inflater.inflate(R.layout.fragment_about, container, false);
    initFonts();
    return rootView;
}

但实际上,这可能会更好地写成这样:

public class AboutFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_about, container, false);
        initFonts(rootView);
        return rootView;
    }

    public void initFonts(final View rootView) {
        TextView title = (TextView) rootView.findViewById(R.id.tv_about_title);
        Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "TRACK.OTF");
        title.setTypeface(font);
        TextView theme = (TextView) rootView.findViewById(R.id.tv_about_theme);
        theme.setTypeface(font);
    }
}

完全避免使用rootView类成员。

如果您没有在 onDestroyViewnullrootView 的引用,那么当 fragment 被放置在返回堆栈上,即使此时它没有 View 。

关于java - 在fragment android中设置自定义字体时出现异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21590363/

相关文章:

java - JPanel 不想聚焦所以 KeyAdapter 不起作用

java - 用于更改 Android 应用布局背景颜色的切换按钮

android - RecyclerView 没有滚动到底部

android - 拦截Fragment中的ActionBar Home按钮

java - RCP 使用 Clear Workspace 启动产品

java - 您可以使用增强的 for every 循环将元素添加到数组中吗?

android - 过滤 SimpleAdapter 时出现 IndexOutOfBoundException

java - 通过 NavigationComponent 在 BottomNavigation fragment 之间传递数据

java - @Converter 不适用于 JPA 2.4

android - 如何在从站点下载和解析 XML 文件时一起使用 Retrofit 和 SimpleXML?