1

我在线性布局中有一个简单的文本视图。布局适用于 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)
4

2 回答 2

2

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

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

debuggingData.setText(Integer.toString(...));
于 2013-10-01T19:25:37.530 回答
1

该错误看起来像是您确实将整数传递给setText方法。如果是这种情况,它会尝试在资源字符串中找到 int,然后抛出Exceptionif 找不到它。

于 2013-10-01T19:22:10.380 回答