我使用自定义布局来显示我的ListView项目。 但是当我尝试在RunTime应用程序崩溃时设置字体。 首先,我尝试访问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;
}
因此,当我的活动开始时,我收到一条错误消息,导致应用程序崩溃。在此之前,该应用程序工作正常。 我究竟做错了什么? 我应该能够将不同的textView设置为不同的TypeFaces。这实际上就是我需要的。但我甚至无法一次设置所有textViews ......
请帮忙。 谢谢
UPDATE 日志如下:
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
我不知道如何格式化日志以使其更具可读性:(
此外,我更改了上面的代码以反映最新的尝试(这也给了我这个错误)
答案 0 :(得分:1)
请附上日志。
很可能你在这一行上有一个NullPointerException
:
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/yourfont.ttf");
((TextView)row).setTypeface(face);
convertView
可能是null
,您应该在这种情况下创建一个新的或调用超类的方法getView
(link)