2014-02-05 49 views
0

下面是代碼,它基本上是一個簡單的菜單驅動的應用程序,我試圖在片段中使用自定義字體,並導致問題。 我已經在一個活動中成功地使用了這個字體,這個活動啓動了另一個活動,在這個活動中這個片段被誇大了。 我不確定這是什麼導致問題,請幫助我。設置自定義字體片段時出現異常錯誤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) 

回答

0

在你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

0

在CoronaTheCarnival.java的第27行放置一個斷點。檢查該行中使用的所有引用以確定哪一個引用爲空。

0

你在你的類中聲明rootView,然後在onCreateView方法中重新聲明它,這可能會限制它的範圍,使你試圖在initFont失敗中引用它。在onCreateView

1

刪除您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

相關問題