在片段android中设置自定义字体时出现异常错误

时间:2014-02-05 22:53:58

标签: java android android-fragments

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

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)

4 个答案:

答案 0 :(得分:2)

您永远不会为成员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类成员。

如果你没有null rootView中对onDestroyView的引用,那么当片段放在后面的堆栈上时,你的片段将保留在所有的视图内存中,甚至虽然在这一点上它没有任何意见。

答案 1 :(得分:1)

在initFonts方法中,执行以下操作:

TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);

这里,rootView为null,因为它从未正确初始化。您似乎混淆了2个rootView变量:

View rootView; // this is one variable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_about, container, false);
        // this is not the same rootView !
        // here, rootView != this.rootView
        initFonts();
        return rootView;
    }

public void initFonts(){
    TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);
    // this is like using this.rootView.findViewById
    // ...
}

initFonts方法无法看到onCreateView方法中的rootView,因此它将使用您在onCreateView上声明的视图,第一个视图从未正确初始化。要解决这个问题,您可以将initFonts的代码移回onCreateView,将视图作为initFonts方法的参数传递,或者在onCreateView中初始化变量,如下所示:

this.rootView = rootView;

答案 2 :(得分:0)

在CoronaTheCarnival.java第27行放置一个断点。检查该行中使用的所有引用,以确定哪一个为null。

答案 3 :(得分:0)

您在班级中声明rootView,然后在onCreateView方法中重新声明它,这可能会限制其范围,使您尝试在initFont中引用它失败。移除View

rootView声明的onCreateView部分