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

时间:2017-09-27 08:30:48

标签: android android-arrayadapter custom-font typeface android-typeface

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

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

 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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

在您的代码中尝试此操作。

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

修改

在assets目录中,创建一个新的fonts目录,然后向其添加chawp.tff

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

更改为fonts/chawp.tff