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

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

问题描述:

下面是代码,它基本上是一个简单的菜单驱动的应用程序,我试图在片段中使用自定义字体,并导致问题。 我已经在一个活动中成功地使用了这个字体,这个活动启动了另一个活动,在这个活动中这个片段被夸大了。 我不确定这是什么导致问题,请帮助我。设置自定义字体片段时出现异常错误android

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) 

在你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 
    // ... 
} 

您onCreateView方法里面的rootView是不是由initFonts方法可见的,所以它会使用你onCreateView,第一个,上面宣布查看这是从来没有正确初始化。为了解决这个问题,你可以在onCreateView移动initFonts的代码后面,通过查看你的initFonts方法的参数,或者初始化变量onCreateView这样的:

this.rootView = rootView; 
+0

感谢队友。它解决了它:D – dw19

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

你在你的类中声明rootView,然后在onCreateView方法中重新声明它,这可能会限制它的范围,使你试图在initFont失败中引用它。在onCreateView

删除您rootView声明的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 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类成员。

如果不这样做nullonDestroyView提及rootView那么你的片段将保留所有的视图内存,当片段被放在后面堆栈上,即使在这一点上不会有任何看法。

+0

最后一部分可能是最好的办法完成此...伟大的解决方案,+1 – 2Dee