2017-04-05 58 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; 
    } 
    

希望这将有助于〜