我有一个ListVIEw活动.在每个列表项中显示TextVIEw.使用这些方法切换所选位置的属性EditText#setTextIsSelectable,EditText#setEllipsize& EditText#setSingleline.点击第1项工作正常.但是在点击另一个项目时我得到的是StackOverFlowError.
下面是我的代码示例.如果我有任何错误,请告诉我.
import androID.app.Activity;import androID.content.Context;import androID.os.Bundle;import androID.text.TextUtils;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.BaseAdapter;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;public class SelectableTextListActivity extends Activity { private static final String TAG = "HistoryActivity"; private ListVIEw ListVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main);//only ListVIEw present in this layout ListVIEw = (ListVIEw) findVIEwByID(R.ID.myList); String items[] = {"Some information Some information Some information", "Some information Some information Some information", "Some information Some information Some information", "Some information Some information Some information", "Some information Some information Some information", "Some information Some information Some information", "Some information Some information Some information", "Some information Some information Some information"}; final MyAdapter myAdapter = new MyAdapter(this, items); ListVIEw.setAdapter(myAdapter); ListVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { Log.d(TAG, "onItemClick: position=" + position); myAdapter.setSelectedposition(position); myAdapter.notifyDataSetChanged(); } }); } 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[] items) { mItems = items; inflater = LayoutInflater.from(context); } public voID setSelectedposition(int mSelectedposition) { this.mSelectedposition = mSelectedposition; } @OverrIDe public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) { VIEwHolder vIEwHolder; if (convertVIEw == null) { convertVIEw = inflater.inflate(R.layout.selectable_text_layout, null, false); vIEwHolder = new VIEwHolder(convertVIEw); convertVIEw.setTag(vIEwHolder); } else { vIEwHolder = (VIEwHolder) convertVIEw.getTag(); } String item = getItem(position); vIEwHolder.selectableTV.setText(item); if (position == mSelectedposition) { Log.d(TAG, "getVIEw() called with: " + "position = [" + position + "], selected = " + true); vIEwHolder.selectableTV.setTextIsSelectable(true); vIEwHolder.selectableTV.setSingleline(false); vIEwHolder.selectableTV.setEllipsize(null); } else { Log.d(TAG, "getVIEw() called with: " + "position = [" + position + "], selected = " + false); vIEwHolder.selectableTV.setTextIsSelectable(false); vIEwHolder.selectableTV.setSingleline(true); vIEwHolder.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; } private class VIEwHolder { public final TextVIEw selectableTV; VIEwHolder(VIEw convertVIEw) { selectableTV = (TextVIEw) convertVIEw.findVIEwByID(R.ID.selectableTextVIEw); } } }}
布局文件:
selectable_text_layout.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:focusable="false" androID:focusableIntouchMode="false" androID:orIEntation="vertical" androID:padding="5dp"> <TextVIEw androID:ID="@+ID/selectableTextVIEw" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:clickable="true" androID:ellipsize="end" androID:focusable="false" androID:text="My app name" androID:textAppearance="?androID:textAppearanceMedium" /></linearLayout>
堆栈跟踪是
D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MBat java.lang.class.isinstance(Class.java:1484)at androID.text.SpannableStringInternal.getSpans(SpannableStringInternal.java:217)at androID.text.SpannedString.getSpans(SpannedString.java:25)at androID.text.SpannableStringInternal.<init>(SpannableStringInternal.java:40)at androID.text.SpannableString.<init>(SpannableString.java:30)at androID.Widget.TextVIEw.removeSuggestionSpans(TextVIEw.java:4200)at androID.Widget.TextVIEw.setText(TextVIEw.java:3926)at androID.Widget.TextVIEw.setText(TextVIEw.java:3911)at androID.Widget.TextVIEw.getIterableTextForAccessibility(TextVIEw.java:9047)at androID.vIEw.VIEw.onInitializeAccessibilityEventInternal(VIEw.java:5450)at androID.vIEw.VIEw.onInitializeAccessibilityEvent(VIEw.java:5422)at androID.Widget.TextVIEw.onInitializeAccessibilityEvent(TextVIEw.java:8402)at androID.vIEw.VIEw.sendAccessibilityEventUncheckedInternal(VIEw.java:5293)at androID.vIEw.VIEw.sendAccessibilityEventUnchecked(VIEw.java:5280)at androID.vIEw.VIEw.sendAccessibilityEventInternal(VIEw.java:5257)at androID.vIEw.VIEw.sendAccessibilityEvent(VIEw.java:5226)at androID.Widget.TextVIEw.sendAccessibilityEvent(TextVIEw.java:8571)at androID.Widget.TextVIEw.onSelectionChanged(TextVIEw.java:7584)at androID.Widget.TextVIEw.spanChange(TextVIEw.java:7784)at androID.Widget.TextVIEw$ChangeWatcher.onSpanAdded(TextVIEw.java:9478)at androID.text.SpannableStringInternal.sendSpanAdded(SpannableStringInternal.java:314)at androID.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:138)at androID.text.SpannableString.setSpan(SpannableString.java:46)at androID.text.SpannableStringInternal.<init>(SpannableStringInternal.java:52)at androID.text.SpannableString.<init>(SpannableString.java:30)at androID.Widget.TextVIEw.removeSuggestionSpans(TextVIEw.java:4200)at androID.Widget.TextVIEw.setText(TextVIEw.java:3926)at androID.Widget.TextVIEw.setText(TextVIEw.java:3911)at androID.Widget.TextVIEw.getIterableTextForAccessibility(TextVIEw.java:9047)at androID.vIEw.VIEw.onInitializeAccessibilityEventInternal(VIEw.java:5450)at androID.vIEw.VIEw.onInitializeAccessibilityEvent(VIEw.java:5422)at androID.Widget.TextVIEw.onInitializeAccessibilityEvent(TextVIEw.java:8402)at androID.vIEw.VIEw.sendAccessibilityEventUncheckedInternal(VIEw.java:5293)at androID.vIEw.VIEw.sendAccessibilityEventUnchecked(VIEw.java:5280)at androID.vIEw.VIEw.sendAccessibilityEventInternal(VIEw.java:5257)at androID.vIEw.VIEw.sendAccessibilityEvent(VIEw.java:5226)at androID.Widget.TextVIEw.sendAccessibilityEvent(TextVIEw.java:8571)at androID.Widget.TextVIEw.onSelectionChanged(TextVIEw.java:7584)at androID.Widget.TextVIEw.spanChange(TextVIEw.java:7784)at androID.Widget.TextVIEw$ChangeWatcher.onSpanAdded(TextVIEw.java:9478)at androID.text.SpannableStringInternal.sendSpanAdded(SpannableStringInternal.java:314)at androID.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:138)at androID.text.SpannableString.setSpan(SpannableString.java:46)at androID.text.SpannableStringInternal.<init>(SpannableStringInternal.java:52)at androID.text.SpannableString.<init>(SpannableString.java:30)at androID.Widget.TextVIEw.removeSuggestionSpans(TextVIEw.java:4200)at androID.Widget.TextVIEw.setText(TextVIEw.java:3926)at androID.Widget.TextVIEw.setText(TextVIEw.java:3911)at androID.Widget.TextVIEw.getIterableTextForAccessibility(TextVIEw.java:9047)at androID.vIEw.VIEw.onInitializeAccessibilityEventInternal(VIEw.java:5450)at androID.vIEw.VIEw.onInitializeAccessibilityEvent(VIEw.java:5422)at androID.Widget.TextVIEw.onInitializeAccessibilityEvent(TextVIEw.java:8402)at androID.vIEw.VIEw.sendAccessibilityEventUncheckedInternal(VIEw.java:5293)at androID.vIEw.VIEw.sendAccessibilityEventUnchecked(VIEw.java:5280)at androID.vIEw.VIEw.sendAccessibilityEventInternal(VIEw.java:5257)at androID.vIEw.VIEw.sendAccessibilityEvent(VIEw.java:5226)at androID.Widget.TextVIEw.sendAccessibilityEvent(TextVIEw.java:809-13 00:48:45.483 8705-8705/club.apptu.edittextlocalhistory D/Error: ERR: TOTAL BYTES WRITTEN: 1615617209-13 00:48:45.483 8705-8705/club.apptu.edittextlocalhistory E/JavaBinder: !!! Failed BINDER TRANSACTION !!!09-13 00:48:45.484 8705-8705/club.apptu.edittextlocalhistory E/AndroIDRuntime: Error reporting crash androID.os.TransactionToolargeExceptionat androID.os.BinderProxy.transactNative(Native Method)at androID.os.BinderProxy.transact(Binder.java:496)at androID.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4144)at com.androID.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
提前致谢.
编辑:
我在ListVIEw#onItemClickListener中调用notifyDatasetChanged时遇到问题.
解决方法:
最后我得到了解决问题的方法.我删除了MyAdapter中的VIEwHolder模式
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; }}
但是在使用VIEwHolder模式时仍然没有找到问题的原因.如果有人找到原因让我知道.
总结以上是内存溢出为你收集整理的android – java.lang.StackOverflowError:堆栈大小为8MB,同时在ListView适配器中启用/禁用TextView#setTextIsSelectable全部内容,希望文章能够帮你解决android – java.lang.StackOverflowError:堆栈大小为8MB,同时在ListView适配器中启用/禁用TextView#setTextIsSelectable所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)