我正在尝试向按钮名称添加文本,例如按钮" ButtonName" be" ButtonNameNow"由button.append("now")
提供未捕获的异常
有没有任何示例或原因我为什么这样做我已搜索过例如找不到任何内容我也没有理解关于Button附加的Google文档,但我之前在Textview上测试过它
E/UncaughtException: java.lang.IndexOutOfBoundsException
at android.graphics.Paint.getTextRunAdvances(Paint.java:1908)
at android.text.TextLine.handleText(TextLine.java:747)
at android.text.TextLine.handleRun(TextLine.java:898)
at android.text.TextLine.measureRun(TextLine.java:414)
at android.text.TextLine.measure(TextLine.java:293)
at android.text.TextLine.metrics(TextLine.java:267)
at android.text.Layout.getLineExtent(Layout.java:998)
at android.text.Layout.drawText(Layout.java:329)
at android.widget.Editor.drawHardwareAccelerated(Editor.java:1401)
at android.widget.Editor.onDraw(Editor.java:1324)
at android.widget.TextView.onDraw(TextView.java:5259)
at android.view.View.draw(View.java:14520)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.getDisplayList(View.java:13399)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.getDisplayList(View.java:13399)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.getDisplayList(View.java:13399)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1379)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.getDisplayList(View.java:13399)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.draw(View.java:14523)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.getDisplayList(View.java:13399)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.getDisplayList(View.java:13399)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.getDisplayList(View.java:13399)
at android.view.View.getDisplayList(View.java:13446)
at android.view.View.draw(View.java:14237)
at android.view.ViewGroup.drawChild(ViewGroup.java:3119)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.draw(View.java:14523)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2386)
at android.view.View.getDisplayList(View.java:13404)
at android.view.View.getDisplayList(View.java:13446)
at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1609)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1484)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2476)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2340)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1963)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1053)
at android.vi
答案 0 :(得分:2)
button.setText(button.getText()+"now");
答案 1 :(得分:1)
我有同样的问题。如果将文本附加到Button,则应用程序崩溃,并出现java.lang.IndexOutOfBoundsException致命错误。我在 Android 6(API23)上发现了 only 这个问题。
解决方法是:
String noItems = String.valueOf(tokens.length) + " " + getResources().getString(R.string.items);
lastConnectedItems.setText(noItems);
代替:
lastConnectedItems.setText(String.valueOf(tokens.length));
lastConnectedItems.append(" ");
lastConnectedItems.append(getResources().getString(R.string.share));
lastConnectedItems是Button。
希望它能帮助到别人:)