如何在editText.setText()中插入一个字符串

时间:2018-01-03 20:31:27

标签: java android android-edittext

你好,我有一个问题,就是我在标题中提出的问题。 我有这个例子,我从我的代码中提取

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) 

2 个答案:

答案 0 :(得分:3)

检查你的布局xml文件,你的editText变量是null因为你的引用" idField"是不正确的。

在获得您的观点参考之前,不要忘记致电setContentView(R.layout.yourlayout)

更新

您忘了拨打setContentView方法

 protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout_file);
  ...

答案 1 :(得分:0)

String实现CharSequence,因此String.valueOf(aInt)确实可以作为参数。