java.lang.StackOverflowError: stack size 8MB while Enabling/Disabling TextView#setTextIsSelectable in ListView Adapter

后端 未结 4 1472
粉色の甜心
粉色の甜心 2021-02-15 17:48

I have an activity with ListView . Displaying the TextView in each list item. Switching the properties of selected position using these methods E

相关标签:
4条回答
  • 2021-02-15 17:53

    i was calling onBackPressed() recursively and that caused StackOverFlowError for me.

    0 讨论(0)
  • 2021-02-15 18:01

    To much of iteration on list scroll will produce this error.. Avoid that...

    0 讨论(0)
  • 2021-02-15 18:11

    Finally I got the solution for my problem. I removed ViewHolder pattern in MyAdapter like

    private static class MyAdapter extends BaseAdapter {
        private static final String TAG = "HistoryAdapter";
        private final LayoutInflater inflater;
        private int mSelectedPosition = -1;
        private String[] mItems;
    
        public MyAdapter(Context context, String[] mItems) {
            this.mItems = mItems;
            inflater = LayoutInflater.from(context);
        }
    
    
    
        public void setSelectedPosition(int mSelectedPosition) {
            this.mSelectedPosition = mSelectedPosition;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            convertView = inflater.inflate(R.layout.selectable_text_layout, null, false);
            TextView selectableTV = (TextView) convertView.findViewById(R.id.selectableTextView);
    
            selectableTV.setText(getItem(position));
    
            if (position == mSelectedPosition) {
                Log.d(TAG, "getView() called with: " + "position = [" + position + "], selected = " + true);
                selectableTV.setTextIsSelectable(true);
                selectableTV.setSingleLine(false);
                selectableTV.setEllipsize(null);
            } else {
                Log.d(TAG, "getView() called with: " + "position = [" + position + "], selected = " + false);
                selectableTV.setTextIsSelectable(false);
                selectableTV.setSingleLine(true);
                selectableTV.setEllipsize(TextUtils.TruncateAt.END);
            }
    
            return convertView;
        }
    
    
        @Override
        public String getItem(int position) {
            return mItems[position];
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public int getCount() {
            return mItems.length;
        }
    }
    

    But still didn't find the the reason for the problem when using ViewHolder pattern.If anybody find the reason let me know.

    0 讨论(0)
  • 2021-02-15 18:20

    The best way to find the error is use a lot of logcat in your code and find where the logcat is not showing. But you can try to change your code like this:

    1. delete the inflater = LayoutInflater.from(context); in MyAdapter constructor
    2. move it in getview method like this:

      if (convertView == null) {
          LayoutInflater inflater = ((Activity) context).getLayoutInflater();
          convertView=inflater.inflate(R.layout.selectable_text_layout, null,false);</br>
          viewHolder = new ViewHolder(convertView);
          convertView.setTag(viewHolder);
      }
      
    0 讨论(0)
提交回复
热议问题