1

我使用自定义布局来显示我的 ListView 项目。但是当我尝试在运行时设置字体时应用程序崩溃。首先,我尝试在 onCreate 中访问布局的 Texviews... 看到它不起作用,我在网上寻找答案,发现我应该在我的自定义适配器中进行设置。我也试过了,结果一样。

我的适配器如下所示:

public class VizitaAdapter extends ArrayAdapter<Vizita>{
    Context context;
    int layoutResourceId;   
    Typeface fontux = null;

  public VizitaAdapter(Context context, int layoutResourceId, Vizita[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        fontux = Typeface.createFromAsset(context.getAssets(), "fonts/myrprocond.otf"); 
    }


 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        WeatherHolder holder = null;
 ...
 ...
        ((TextView)row).setTypeface(fontux);
        return row;
 }

因此,当我的活动应该开始时,我会收到一条导致应用程序崩溃的错误消息。在此之前,该应用程序运行良好。我究竟做错了什么?我应该能够将不同的 textViews 设置为不同的 TypeFaces。这实际上是我需要的。但我什至不能一次设置所有的文本视图......

请帮忙。谢谢

更新日志如下

08-01 16:57:22.727: D/szipinf(881): Initializing inflate state 08-01 16:57:22.787: D/AndroidRuntime(881): Shutting down VM 08-01 16:57:22.787: W/dalvikvm(881): threadid=1: thread exiting with uncaught exception (group=0x40015560) 08-01 16:57:22.797: E/AndroidRuntime(881): FATAL EXCEPTION: main 08-01 16:57:22.797: E/AndroidRuntime(881): java.lang.ClassCastException: android.widget.RelativeLayout 08-01 16:57:22.797: E/AndroidRuntime(881): at softwarex.ssmmonitor_custom.VizitaAdapter.getView(VizitaAdapter.java:50) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.AbsListView.obtainView(AbsListView.java:1430) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.makeAndAddView(ListView.java:1745) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.fillDown(ListView.java:670) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.fillFromTop(ListView.java:727) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.ListView.layoutChildren(ListView.java:1598) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.AbsListView.onLayout(AbsListView.java:1260) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.View.layout(View.java:7175) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.os.Handler.dispatchMessage(Handler.java:99) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.os.Looper.loop(Looper.java:123) 08-01 16:57:22.797: E/AndroidRuntime(881): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-01 16:57:22.797: E/AndroidRuntime(881): at java.lang.reflect.Method.invokeNative(Native Method) 08-01 16:57:22.797: E/AndroidRuntime(881): at java.lang.reflect.Method.invoke(Method.java:507) 08-01 16:57:22.797: E/AndroidRuntime(881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-01 16:57:22.797: E/AndroidRuntime(881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-01 16:57:22.797: E/AndroidRuntime(881): at dalvik.system.NativeStart.main(Native Method) 08-01 16:57:24.657: I/Process(881): Sending signal. PID: 881 SIG: 9

我不知道如何格式化日志以使其更具可读性:(

我还更改了上面的代码以反映最新的尝试(这也给了我这个错误)

4

1 回答 1

1

请附上日志。很可能你NullPointerException在这条线上有一个:

Typeface face=Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf"); 
((TextView)row).setTypeface(face);

convertView可能是null,在这种情况下您应该创建一个新的或调用getView超类的方法(链接

于 2013-08-01T15:52:23.853 回答