android

时间:2015-06-03 09:40:56

标签: android nullpointerexception android-arrayadapter

在arrayadapter上添加数据时出现空指针异常。得到错误

  

E / AndroidRuntime(9233):致命异常:主

     

E / AndroidRuntime(9233):java.lang.NullPointerException E / AndroidRuntime(9233):at   co.adapter.TopicCustomDisplayArrayAdapter.getView(TopicCustomDisplayArrayAdapter.java:415)

     

E / AndroidRuntime(9233):at co.utils.TwoWayView.obtainView(TwoWayView.java:5599)   E / AndroidRuntime(9233):at   co.utils.TwoWayView.onMeasure(TwoWayView.java:3899)   E / AndroidRuntime(9233):在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()。height = heightTextAd;

知道如何更正此错误。这里的arraylist大小是5。

1 个答案:

答案 0 :(得分:3)

似乎getLayoutParams()导致nullpointer,因为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);