android TextView奇怪的行为

时间:2013-10-01 19:14:05

标签: android android-layout textview

我在linearlayout中有一个简单的textview。布局适用于ListView中的项目。我使用textView作为调试工具:跟踪数据。如果我在视图上执行getText(),一切都很好。但如果我做setText,我会得到一个例外。所以我把这两条线依次放在一起。 setText仍然抛出异常。怎么可能?如果资源有问题,getText应该失败。但它打印得很好。

代码段:

 print(debuggingData.getText());//goes through fine
 debuggingData.setText("some text");//throws exception

错误:

 FATAL EXCEPTION: main
 android.content.res.Resources$NotFoundException: String resource ID #0x0
    at android.content.res.Resources.getText(Resources.java:1057)
    at android.widget.TextView.setText(TextView.java:4111)
    at com.mycompany.mygame.adapter.MyListAdapter.getView(MyListAdapter.java:115)
    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
    at android.widget.AbsListView.obtainView(AbsListView.java:2613)
    at android.widget.ListView.makeAndAddView(ListView.java:1838)
    at android.widget.ListView.fillDown(ListView.java:681)
    at android.widget.ListView.fillSpecific(ListView.java:1339)
    at android.widget.ListView.layoutChildren(ListView.java:1637)
    at android.widget.AbsListView.onLayout(AbsListView.java:2431)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at com.slidingmenu.lib.CustomViewAbove.onLayout(CustomViewAbove.java:476)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14482)
    at android.view.ViewGroup.layout(ViewGroup.java:4469)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2151)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1970)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1183)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4863)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5328)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您似乎在尝试显示整数而不先将其转换为字符串。  setText(0)查找资源ID为0的字符串并尝试加载它,如果不能,则会抛出异常。

要避免此问题,只需将整数转换为Integer.toString()的字符串:

debuggingData.setText(Integer.toString(...));

答案 1 :(得分:1)

错误看起来像是你真的将整数传递给setText方法。如果是这种情况,它会尝试在资源字符串中找到int,如果找不到则抛出Exception