Android TextView、setText 使用多个变量连接在一起
我正在从数据库中提取数据,返回类实例中的值。在 Activity 的 onCreate 方法中,我设置内容视图,然后运行以下代码:
val thisObject:ClassCardJ = myDBAdapter.getAddressBookEntry(cardNum)
//val textFullName:String = (thisObject.getfName().toString() + " " + thisObject.getlName().toString())
val textFirstName:String = thisObject.getfName().toString()
val textLastName:String = thisObject.getlName().toString()
firstLine.setText(textFirstName)
Toast.makeText(AddressBookEntryActivity.this, thisObject.getlName().toString(), Toast.LENGTH_SHORT).show();
它将firstLine 文本设置为textFirstName 值,并使用我在textLastName 中使用的值显示一个toast。
但是,当我尝试将firstLine 的setText 更改为textFullName(取消注释后)时,我的程序强制关闭。我做错了什么有趣的事情吗?
根据请求,我的 logcat 堆栈跟踪(我尝试包含所有相关的内容并省略所有不相关的内容):
I/ActivityManager( 67): Process com.android.email (pid 330) has died.
I/ActivityManager( 67): Starting: Intent { cmp=me.snapcard/.AddressBookActivity } from pid 393
I/ActivityManager( 67): Displayed me.snapcard/.AddressBookActivity: +589ms
I/ActivityManager( 67): Starting: Intent { cmp=me.snapcard/.AddressBookEntryActivity (has extras) } from pid 393
E/dalvikvm( 393): Could not find class 'scala.collection.mutable.StringBuilder', referenced from method me.snapcard.AddressBookEntryActivity.onCreate
W/dalvikvm( 393): VFY: unable to resolve new-instance 1148 (Lscala/collection/mutable/StringBuilder;) in Lme/snapcard/AddressBookEntryActivity;
D/dalvikvm( 393): VFY: replacing opcode 0x22 at 0x0050
D/dalvikvm( 393): VFY: dead code 0x0052-0091 in Lme/snapcard/AddressBookEntryActivity;.onCreate (Landroid/os/Bundle;)V
D/AndroidRuntime( 393): Shutting down VM
W/dalvikvm( 393): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 393): FATAL EXCEPTION: main
E/AndroidRuntime( 393): java.lang.NoClassDefFoundError: scala.collection.mutable.StringBuilder
E/AndroidRuntime( 393): at me.snapcard.AddressBookEntryActivity.onCreate(AddressBookEntryActivity.scala:40)
E/AndroidRuntime( 393): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 393): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 393): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 393): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 393): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 393): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 393): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 393): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 67): Force finishing activity me.snapcard/.AddressBookEntryActivity
W/ActivityManager( 67): Force finishing activity me.snapcard/.AddressBookActivity
W/ActivityManager( 67): Activity pause timeout for HistoryRecord{4082ce18 me.snapcard/.AddressBookEntryActivity}
I/dalvikvm( 67): Jit: resizing JitTable from 1024 to 2048
W/ActivityManager( 67): Launch timeout has expired, giving up wake lock!
I am pulling data from a database, returning the values in an instance of a class. In my onCreate method of my Activity, I am setting the content view and then running the following code:
val thisObject:ClassCardJ = myDBAdapter.getAddressBookEntry(cardNum)
//val textFullName:String = (thisObject.getfName().toString() + " " + thisObject.getlName().toString())
val textFirstName:String = thisObject.getfName().toString()
val textLastName:String = thisObject.getlName().toString()
firstLine.setText(textFirstName)
Toast.makeText(AddressBookEntryActivity.this, thisObject.getlName().toString(), Toast.LENGTH_SHORT).show();
Which sets the firstLine text to the textFirstName value and displays a toast with the value that I am using in textLastName.
However, when I attempt to change the setText of the firstLine to textFullName (after uncommenting it), my program force closes. Am I doing something funny wrong??
As request, my logcat stack trace (I tried to include everything relevant and omit everything non-relevant):
I/ActivityManager( 67): Process com.android.email (pid 330) has died.
I/ActivityManager( 67): Starting: Intent { cmp=me.snapcard/.AddressBookActivity } from pid 393
I/ActivityManager( 67): Displayed me.snapcard/.AddressBookActivity: +589ms
I/ActivityManager( 67): Starting: Intent { cmp=me.snapcard/.AddressBookEntryActivity (has extras) } from pid 393
E/dalvikvm( 393): Could not find class 'scala.collection.mutable.StringBuilder', referenced from method me.snapcard.AddressBookEntryActivity.onCreate
W/dalvikvm( 393): VFY: unable to resolve new-instance 1148 (Lscala/collection/mutable/StringBuilder;) in Lme/snapcard/AddressBookEntryActivity;
D/dalvikvm( 393): VFY: replacing opcode 0x22 at 0x0050
D/dalvikvm( 393): VFY: dead code 0x0052-0091 in Lme/snapcard/AddressBookEntryActivity;.onCreate (Landroid/os/Bundle;)V
D/AndroidRuntime( 393): Shutting down VM
W/dalvikvm( 393): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 393): FATAL EXCEPTION: main
E/AndroidRuntime( 393): java.lang.NoClassDefFoundError: scala.collection.mutable.StringBuilder
E/AndroidRuntime( 393): at me.snapcard.AddressBookEntryActivity.onCreate(AddressBookEntryActivity.scala:40)
E/AndroidRuntime( 393): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 393): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 393): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 393): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime( 393): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 393): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 393): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 393): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 67): Force finishing activity me.snapcard/.AddressBookEntryActivity
W/ActivityManager( 67): Force finishing activity me.snapcard/.AddressBookActivity
W/ActivityManager( 67): Activity pause timeout for HistoryRecord{4082ce18 me.snapcard/.AddressBookEntryActivity}
I/dalvikvm( 67): Jit: resizing JitTable from 1024 to 2048
W/ActivityManager( 67): Launch timeout has expired, giving up wake lock!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像你说的,你使用了 StringBuilder。我怀疑您的 scala.collection.mutable 对象的编码不允许其项目顺利串联。我强烈建议您将连接的字符串(仅包括 textFirstName 和 textLastName,如果您要单独使用它们)包装在 Scala StringBuilder 中。如果你想使用java.lang.StringBuilder,那么你可以通过简单地用java.lang.Stringbuilder包装Scala StringBuilder来转换Scala StringBuilder,详细信息此处。希望有帮助!
Like you said, you used a StringBuilder. My suspicion is that your scala.collection.mutable object has encoding that does not allow smooth concatenation of its items. I strongly suggest you wrap the concatenated string (including just textFirstName and textLastName, if you're going to use them individually) in a Scala StringBuilder. If you would like to use the java.lang.StringBuilder, then you can convert a Scala StringBuilder by simply wrapping the Scala StringBuilder with the java.lang.Stringbuilder, which is detailed here. Hope that helped!