如何在editText.setText()中插入一个字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在editText.setText()中插入一个字符串相关的知识,希望对你有一定的参考价值。
你好我有一个问题,我把它放在标题中。我有这个例子,我从我的代码中提取
String text = "Test";
EditText editText = (EditText) findViewById(R.id.idField);
editText.setText(text);
并且我无法将int转换为charSequence我的目标是将一个字符串放入editText.setText();
这是我的onCreate:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
try {
fileExist();
sessionTimeOut();
passwordValidation(getLocalPassword());
} catch (FileNotExistException e) {
setLoginActivity();
} catch (SessionTimeOutException e) {
String text="Sometext";
EditText editText = (EditText) findViewById(R.id.usernameField);
setLoginActivity(R.string.sessionOut);
editText.setText(text);
} catch (PasswordInvalidException e) {
setLoginActivity(R.string.criterios);
}
setWelcomeBackLayout();
}
该错误位于editText.setText(text);位于
有我的错误:
E/androidRuntime: FATAL EXCEPTION: main
Process: pt.edu.es_loule, PID: 3498
java.lang.RuntimeException: Unable to start activity ComponentInfo{pt.edu.es_loule/pt.edu.es_loule.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at pt.edu.es_loule.LoginActivity.onCreate(LoginActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案
检查你的布局xml文件,你的editText变量为null,因为你的引用“idField”不正确。
在获得您的观点参考之前,不要忘记致电setContentView(R.layout.yourlayout)
。
UPDATE
你忘了打电话给setContentView
方法
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout_file);
...
另一答案
String实现CharSequence,因此String.valueOf(aInt)确实可以作为参数。
以上是关于如何在editText.setText()中插入一个字符串的主要内容,如果未能解决你的问题,请参考以下文章
EditText.SetText() 在自定义适配器中更改我的软键盘输入类型
DialogFragment - 访问布局组件的位置[即editText.setText(“”)]