-5

It call a json file and shows data via RecyclerView.

I want use custom font in fragment.

this is the structure of 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"/>

I do these steps:

I put the fonts in src/main/assets/fonts folder.

fragment 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);

The app will be compiled but it crashed when I run it on a device.

what is my mistake?

log:

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
elma
  • 53
  • 8
  • please post crash log – Linh Apr 05 '17 at 01:34
  • That's not the stack trace from your crash. You're likely getting a `NullPointerException`, since you're looking for the `TextView` with ID `movie_title` in the `View` created from the `fragment_tab_2` layout, but it's not there. It's in the `service_list` layout. If that's the layout for your `RecyclerView` items, you need to set the typeface in the `Adapter`, not in the `Fragment`'s `onCreate()`. – Mike M. Apr 05 '17 at 01:54

1 Answers1

0
  1. Make sure TextView(movie_title) is exist in your fragment layout(fragment_tab_2.xml).

  2. In Typeface.createFromAsset(), use getActivity().getAssets() instead of textView.getContext().getAssets()

    Typeface face = Typeface.createFromAsset(getActivity().getAssets(), "fonts/custom_sans.ttf");
    textView.setTypeface(face);
    
  3. Use inflater.inflate(R.layout.fragment_tab_2, container, null) instead of inflater.inflate(R.layout.fragment_tab_2, null).

  4. Finally, from fragment onCreateView(), must return 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;
    }
    

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61