2017-04-05 109 views
-5

它調用json文件並通過RecyclerView顯示數據。android自定義字體使應用程序崩潰

我想在片段中使用自定義字體。

這是XML的結構。

MainActivity.xml ...

<android.support.v4.view.ViewPager 
    android:id="@+id/vp_horizontal_ntb" 

...

fragment_tab_2.xml

<android.support.v7.widget.RecyclerView 
    android:id="@+id/main_recycler" 
    ... 
    tools:listitem="@layout/service_list"/> 

service_list.xml

<TextView 
     android:id="@+id/movie_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="This is a title"/> 

我做這些步驟:

我把字體中的src/main /資產/ fonts文件夾中。

片段的java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View x = inflater.inflate(R.layout.fragment_tab_2,null); 

     TextView textView = (TextView) x.findViewById(R.id.movie_title); 
     Typeface face = Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/custom_sans.ttf"); 
     textView.setTypeface(face); 

該應用程序將被編譯,但它墜毀時,我的設備上運行。

我的錯誤是什麼?

日誌:

04-05 06:11:11.237 30326-30326/? I/art: Late-enabling -Xcheck:jni 
04-05 06:11:11.298 30326-30339/com.test.teb E/HAL: load: id=gralloc != hmi->id=gralloc 
04-05 06:11:11.315 30326-30326/com.test.teb W/System: ClassLoader referenced unknown path: /data/app/com.test.teb-2/lib/arm64 
04-05 06:11:11.336 30326-30326/com.test.teb I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl 
04-05 06:11:11.347 30326-30326/com.test.teb W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
04-05 06:11:11.509 30326-30326/com.test.teb I/HwSecImmHelper: mSecurityInputMethodService is null 
04-05 06:11:11.892 30326-30326/com.test.teb I/Process: Sending signal. PID: 30326 SIG: 9 
+0

請張貼崩潰日誌 –

+0

這不是從崩潰的堆棧跟蹤。你可能會得到一個'NullPointerException',因爲你正在尋找''View_''中的ID爲'movie_title'的'TextView',它由'fragment_tab_2'佈局創建,但它不在那裏。它在'service_list'佈局中。如果這是'RecyclerView'項目的佈局,則需要在'Adapter'中設置字體,而不是在'Fragment'的'onCreate()'中設置字體。 –

回答

0
  1. 確保TextView的(movie_title)在片段佈局(fragment_tab_2.xml)是存在的。

  2. Typeface.createFromAsset(),使用getActivity().getAssets()代替textView.getContext().getAssets()

    Typeface face = Typeface.createFromAsset(getActivity().getAssets(), "fonts/custom_sans.ttf"); 
    textView.setTypeface(face); 
    
  3. 使用inflater.inflate(R.layout.fragment_tab_2, container, null),而不是inflater.inflate(R.layout.fragment_tab_2, null)

  4. 最後,從片段onCreateView()必須返回View x

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        View x = inflater.inflate(R.layout.fragment_tab_2, container, null); 
    
        TextView textView = (TextView) x.findViewById(R.id.movie_title); 
    
        Typeface face = Typeface.createFromAsset(getActivity().getAssets(), "fonts/custom_sans.ttf"); 
        textView.setTypeface(face); 
    
        .......... 
        ............... 
    
        return x; 
    } 
    

希望這將有助於〜

相關問題