在arrayadapter上添加数据时出现空指针异常。得到误差
E/AndroidRuntime(9233): FATAL EXCEPTION: main
E/AndroidRuntime (9233): . lang。NullPointerException E/AndroidRuntime(9233): atco.adapter.TopicCustomDisplayArrayAdapter.getView (TopicCustomDisplayArrayAdapter.java: 415)
E/AndroidRuntime(9233): at co.util . twowayview . obtainview (TwoWayView.java:5599)E/AndroidRuntime (9233):co.utils.TwoWayView.onMeasure (TwoWayView.java: 3899)E/AndroidRuntime(9233): at android.view.View.measure(View.java:12773)
在我的代码中这样做:
private View convertView;
Holder holderText = null;
public TopicCustomDisplayArrayAdapter(Context context,
ArrayList<fieldModel> arrayList) {
super(context, R.layout.custom_display, arrayList);
mInflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
customTopicAd = arrayList;
mcontext = context;
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();
}
@Override
public View getView(final int position, View cv, ViewGroup parent) {
convertView = cv;
AdTypeString = customTopic.get(position).getAd_Type();
if (AdTypeString.equalsIgnoreCase("Text Ads")) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.text_view,
parent, false);
holderText = new Holder();
holderText.textRelativeLayout = (RelativeLayout) convertView
.findViewById(R.id.relativeTextAdView);
convertView.setTag(holderText);
} else {
holderText = (Holder) convertView.getTag();
}
int widthTextAd = Integer.parseInt(customTopicAd.get(position)
.getWidht());
int heightTextAd = Integer.parseInt(customTopicAd.get(position)
.getHeight();
holderText.textRelativeLayout.getLayoutParams().width = widthTextAd;
holderText.textRelativeLayout.getLayoutParams().height = heightTextAd;
} else if (AdTypeString.equalsIgnoreCase("Image Ads")) {
// display another layout
}
return convertView;
}
public static class Holder {
// Display Ads Resrouces
public RelativeLayout textRelativeLayout;
}
高度和宽度正常,但无法设置相对布局的高度和宽度。进入这一行:
holderText.textRelativeLayout.getLayoutParams()。width = widthTextAd;holderText.textRelativeLayout.getLayoutParams()。
知道如何纠正这个错误吗?这里数组列表的大小是5
似乎getLayoutParams()
导致空指针,因为textRelativeLayout
没有任何布局参数或任何大小。如果你想设置textRelativeLayout的大小,你可以考虑设置这个视图的layoutParams。所以改变
holderText.textRelativeLayout.getLayoutParams().width = widthTextAd;
holderText.textRelativeLayout.getLayoutParams().height = heightTextAd;
TwoWayView.LayoutParams lp = new TwoWayView.LayoutParams(widthTextAd, heightTextAd);
holderText.textRelativeLayout.setLayoutParams(lp);