Search code examples
androidandroid-arrayadaptercustom-fonttypefaceandroid-typeface

Adding a new font in adapter class using " getContext().getAssets() ", crashes my application


adding a new font in adapter class using " getContext().getAssets() ", crashes my application

When I remove the two lines for customizing the font, my application runs just fine. But when I include those lines my application crashes. Is there any other way to add custom fonts? What am I doing wrong here?

 public class CustomAdapter extends ArrayAdapter<String> {

        int images[];

        public CustomAdapter(@NonNull Context context, String[] names, int pics[]) {
            super(context, R.layout.face_list, names);
            this.images = pics;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            LayoutInflater myInflater = LayoutInflater.from(getContext());
            View listDisplay = myInflater.inflate(R.layout.face_list, parent, false);

            ImageView face = listDisplay.findViewById(R.id.face);
            TextView name = listDisplay.findViewById(R.id.name);


    Typeface chawpFont = Typeface.createFromAsset(listDisplay.getContext().getAssets(),"chawp.tff");
            name.setTypeface(chawpFont);

            String participant_name = (String) getItem(position);
            int participant_pic = images[position];



            face.setImageResource(participant_pic);
            name.setText(participant_name);

            return listDisplay;
        }
    }

Error Log :

09-27 14:43:21.029 14524-14731/com.mohdfaheemkhan.wellgrounded D/mali_winsys: new_window_surface returns 0x3000
09-27 14:43:21.047 14524-14524/com.mohdfaheemkhan.wellgrounded D/AndroidRuntime: Shutting down VM
09-27 14:43:21.049 14524-14524/com.mohdfaheemkhan.wellgrounded E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.mohdfaheemkhan.wellgrounded, PID: 14524
                                                                                 java.lang.RuntimeException: Font asset not found chawp.tff
                                                                                     at android.graphics.Typeface.createFromAsset(Typeface.java:190)
                                                                                     at com.mohdfaheemkhan.wellgrounded.CustomAdapter.getView(CustomAdapter.java:41)
                                                                                     at android.widget.AbsListView.obtainView(AbsListView.java:2474)
                                                                                     at android.widget.ListView.makeAndAddView(ListView.java:1920)
                                                                                     at android.widget.ListView.fillDown(ListView.java:717)
                                                                                     at android.widget.ListView.fillFromTop(ListView.java:778)
                                                                                     at android.widget.ListView.layoutChildren(ListView.java:1715)
                                                                                     at android.widget.AbsListView.onLayout(AbsListView.java:2235)
                                                                                     at android.view.View.layout(View.java:16953)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5579)
                                                                                     at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
                                                                                     at android.view.View.layout(View.java:16953)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5579)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
                                                                                     at android.view.View.layout(View.java:16953)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5579)
                                                                                     at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:434)
                                                                                     at android.view.View.layout(View.java:16953)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5579)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
                                                                                     at android.view.View.layout(View.java:16953)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5579)
                                                                                     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
                                                                                     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
                                                                                     at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
                                                                                     at android.view.View.layout(View.java:16953)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5579)
                                                                                     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
                                                                                     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
                                                                                     at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2785)
                                                                                     at android.view.View.layout(View.java:16953)
                                                                                     at android.view.ViewGroup.layout(ViewGroup.java:5579)
                                                                                     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2552)
                                                                                     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2255)
                                                                                     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
                                                                                     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6708)
                                                                                     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
                                                                                     at android.view.Choreographer.doCallbacks(Choreographer.java:696)
                                                                                     at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                                     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
                                                                                     at android.os.Handler.handleCallback(Handler.java:815)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                     at android.os.Looper.loop(Looper.java:207)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5728)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
09-27 14:43:22.865 14524-14524/com.mohdfaheemkhan.wellgrounded I/Process: Sending signal. PID: 14524 SIG: 9

Solution

  • Try this in your code .

    public class CustomAdapter extends ArrayAdapter {
    
        int images[];
        private Context mContext;
    
        public CustomAdapter(@NonNull Context context, String[] names, int pics[]) {
            super(context, R.layout.face_list, names);
            this.images = pics;
            // add here
            this.mContext = context;
        }
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    
            // edited here , change to mContext
            LayoutInflater myInflater = LayoutInflater.from(mContext);
            View listDisplay = myInflater.inflate(R.layout.face_list, parent, false);
    
            ImageView face = listDisplay.findViewById(R.id.face);
            TextView name = listDisplay.findViewById(R.id.name);
            // edited here , change to mContext
            Typeface chawpFont = Typeface.createFromAsset(mContext.getAssets(), "chawp.tff");
            name.setTypeface(chawpFont);
    
            String participant_name = (String) getItem(position);
            int participant_pic = images[position];
    
            face.setImageResource(participant_pic);
            name.setText(participant_name);
    
            return listDisplay;
        }
    }
    

    Edit

    In the assets directory, create a new fonts directory , then add chawp.tff to it .

    Typeface chawpFont = Typeface.createFromAsset(mContext.getAssets(), "fonts/chawp.tff");

    change to fonts/chawp.tff