简体   繁体   中英

android TextView strange behavior

I have a simple textview in a linearlayout. The layout is for the items in a ListView. I am using the textView as a debugging tool: to track data. If I do getText() on the view everything is fine. But if I do setText I get an exception. So I put the two lines one after the other. setText still throw an exception. How is that possible? If the resource had a problem the getText should fail. But it prints fine.

code snippet:

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

error:

 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)

It seems that you're trying to display an integer without converting it to a string first. setText(0) looks for a string with a resource ID of 0 and tries to load it, and if it can't, it throws an exception.

To avoid this problem, just convert your integer to a string with Integer.toString() :

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

The error looks like you're really passing an integer to setText method. If that's the case, it would try to find the int in the resources strings, throwing the Exception if can't find it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM