setText来自字符串的EditText

时间:2012-10-19 07:30:39

标签: android android-edittext

我试图在EditText中为我的对象分配一些文本。它返回一些运行时异常。 myObj.address有值,我可以通过getAddress.toString()来烘烤它。

         my_edittext = (EditText).findViewById(R.id.myedittext);
         my_edittext.setText(myObj.getAddress().toString());

如何在EditText中设置此文本?我怀疑.setText是否只接受charSequence,那么如何将我的字符串转换为charSequnce?

编辑:我的Logcat

10-19 13:58:40.719: D/AndroidRuntime(4344): Shutting down VM
10-19 13:58:40.719: W/dalvikvm(4344): threadid=1: thread exiting with uncaught exception (group=0x40a0d1f8)
10-19 13:58:40.729: E/AndroidRuntime(4344): FATAL EXCEPTION: main
10-19 13:58:40.729: E/AndroidRuntime(4344): java.lang.NullPointerException
10-19 13:58:40.729: E/AndroidRuntime(4344):     at com.x.y.z.activity.NewEntryActivity$3.onClick(NewEntryActivity.java:677)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at android.view.View.performClick(View.java:3511)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at android.view.View$PerformClick.run(View.java:14105)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at android.os.Handler.handleCallback(Handler.java:605)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at android.os.Looper.loop(Looper.java:137)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at java.lang.reflect.Method.invoke(Method.java:511)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-19 13:58:40.729: E/AndroidRuntime(4344):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

我认为应该是

my_edittext.setText(myObj.getAddress().toString()); 

答案 1 :(得分:1)

         my_edittext = (EditText).findViewById(R.id.myedittext);
         my_edittext.setText(myObj.getAddress().toString());

这实际上应该有效。

首先检查myObj.getAddress是否返回空值,如果然后在其中放入一些值并尝试。

也可以尝试这些 -

伪 -

my_edittext = (EditText).findViewById(R.id.myedittext);
String myString = myObj.setAddress("hello"); // If you have a setter method
my_edittext.setText(myString);