1

I am setting list view based on some data received using Base Adapter.

When I go for list view getting null object reference error in 'int android.view.View.getImportantForAccessibility()'

I have tried the solutions from the following:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference

Finder Function not working, Base Adapter invokes NullPointer

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' using BaseAdapter

Why view return null in android

Custom listView android fails

Even though i am not able understand why I am getting the above error. It would be more helpful, if any one ready to help me. Thanks in advance..

Here is my code:

public class ListViewAdapter extends BaseAdapter
{
    ArrayList<HashMap<String, String>> listAdapter;
    LayoutInflater inflater;
    Context getActivityContext;


    public ListViewAdapter(Activity rAct, ArrayList<HashMap<String, String>> receivedListAdapter, Context inGetActivityContext)
    {
        listAdapter = receivedListAdapter;  
        getActivityContext = inGetActivityContext;
        if(getActivityContext != null)
        {
            this.inflater = (LayoutInflater)
 getActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    }

    @Override
    public int getViewTypeCount()   
    {
        return getCount();
    }

    @Override
    public int getItemViewType(int position)
    {
        return position;
    }

    @Override
    public int getCount()
    {
        return listAdapter .size();
    }

    @Override
    public Object getItem(int arg0)
    {
        return listAdapter .get(arg0);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    public class ViewHolder
    {
        TextView tv1, tv2, tv3, tv4, tv5;
        LinearLayout ll1, ll2;
        ImageView img1, img2, img3, img4, img5;
        FrameLayout fl;
        LinearLayout llrootLayout;
    }

    @Override
    public View getView(final int position, View cv, ViewGroup parent)
    {
        final ViewHolder holder;
        if(cv== null)
        {
            holder = new ViewHolder();
            if(inflater != null)
            {
                cv= inflater.inflate(R.layout.rowview_commonlist_ll, parent, false);
                holder.img1 = (ImageView) cv.findViewById(R.id.images);
                holder.tv1 = (TextView) cv.findViewById(R.id.tvDate);
                holder.ll1 = (LinearLayout) cv.findViewById(R.id.llDummy1);
                holder.ll2 = (LinearLayout) cv.findViewById(R.id.llDummy2);
                holder.img2 = (FrameLayout) cv.findViewById(R.id.imgP);
                holder.img3 = (ImageView) cv.findViewById(R.id.images1);
                holder.img4 = (ImageView) cv.findViewById(R.id.new1);
                holder.img5 = (ImageView) cv.findViewById(R.id.imgvAR);
                holder.tv2 = (TextView) cv.findViewById(R.id.text1);
                holder.tv3 = (TextView) cv.findViewById(R.id.text2);
                holder.tv4 = (TextView) cv.findViewById(R.id.text3);
                holder.llrootLayout = (LinearLayout) cv.findViewById(R.id.rootTxt);
            }
            if(cv!= null && holder != null)
            {
                cv.setTag(holder);
            }
        }

        else
        {
            holder = (ViewHolder) cv.getTag();
        }

        //here some code for assigning UI based on listAdapter data
        if(listAdapter  != null)
        {
            .
            .
            .
        }

       //here some code for click operation of list view item
        .
        .
        .
        .
        .

        return cv;
    }
}
  • 1
    Possible duplicate of [How to solve NullPointerException error in Android?](https://stackoverflow.com/questions/13303544/how-to-solve-nullpointerexception-error-in-android) – Ashish Aug 30 '19 at 10:30
  • If that is indeed where the error is, then you're just returning null from `getView()`, as the first link alludes to. You cannot return null there. You've redacted a bit of code, and you have a large number of unnecessary null checks, so it's hard to follow. You'll just need to figure out why `cv` is null at the end of `getView()`. – Mike M. Aug 30 '19 at 14:00
  • Thank you for your suggestion Mike M., I will check. – Ponnangan Rajumayandi Sep 03 '19 at 02:43

0 Answers0