0

Null pointer exception during adding data on arrayadapter. getting error

E/AndroidRuntime(9233): FATAL EXCEPTION: main

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): at android.view.View.measure(View.java:12773)

Doing this on my code:

 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;


}

height and widht getting properly but could not set height and widht on relative layout. getting on this line:

holderText.textRelativeLayout.getLayoutParams().width = widthTextAd; holderText.textRelativeLayout.getLayoutParams().height = heightTextAd;

Any idea how to correction this error. here arraylist size is 5.

1 Answer 1

3

It seems that getLayoutParams() causes nullpointer because textRelativeLayout has not have any layout params or any size.If you want to set textRelativeLayout's size,you may consider to set layoutParams of this view. So Change

holderText.textRelativeLayout.getLayoutParams().width = widthTextAd;
holderText.textRelativeLayout.getLayoutParams().height = heightTextAd;

to

TwoWayView.LayoutParams lp = new TwoWayView.LayoutParams(widthTextAd, heightTextAd);
holderText.textRelativeLayout.setLayoutParams(lp);
Sign up to request clarification or add additional context in comments.

7 Comments

I added LayoutParams LP = new LayoutParams(widthTextAd, heightTextAd); holderText.textRelativeLayout.setLayoutParams(LP); and still getting same error. if i add your above relative layout code, the casting problem, :java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to co.utils.TwoWayView$LayoutParams
Still getting same error on line: holderText.textRelativeLayout.setLayoutParams(LP);
Use it as TwoWayView.LayoutParams lp = new TwoWayView.LayoutParams(widthTextAd, heightTextAd); see edit.
sorry, it could not help for me. still getting same type of error.i dont know how to get such type of error. thank you for your comments.
i think, holderText not recognized properly.but i defined already, why getting error? if i remove this line of code then next error comes holderText.textviewName. so any idea?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.