使用“getContext().getAssets()”在适配器 class 中添加新字体,使我的应用程序崩溃

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

使用“getContext().getAssets()”在适配器 class 中添加新字体,使我的应用程序崩溃

当我删除用于自定义字体的两行时,我的应用程序运行正常。但是当我包含这些行时,我的应用程序崩溃了。还有其他方法可以添加自定义字体吗?我在这里做错了什么?

 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;
        }
    }

错误日志:

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

在您的代码中试试这个。

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;
    }
}

编辑

在资产目录中,创建一个新的 fonts 目录,然后将 chawp.tff 添加到其中。

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

改为fonts/chawp.tff

不是获取视图上下文,而是从文本视图本身获取上下文

if(name.getContext()!=null){
Typeface chawpFont = Typeface.createFromAsset(name.getContext().getAssets(),"chawp.tff");
name.setTypeface(chawpFont);
}

您也可以尝试使用 parent.getContext(),这是获取上下文的更好方法