Rishav Rishav -4 years ago 52
Android Question

editText.setText isn't working

All i want is to set some text in my

editText
. I dont want to use
android:text="bla bla bla"
in the xml because ive got to something with the user input.
Y is this simple stuff not working for me. My app builds up successfully but crashes.

aaaa.setText("sss");


I tried it on a fresh project just to verify it works. Well it doesnt.

My files:-(of the new project)

package com.rishav.testing;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {

EditText aaaa=(EditText)findViewById(R.id.aaaa);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
aaaa.setText("sss");
}
}


The layout:-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rishav.testing.MainActivity">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="300dp"
android:id="@+id/aaaa"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


By the way i am using AS 3.0 canary 3 sdk tools 26.0.0

Error Logcat:-

06-13 04:02:47.368 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.371 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.373 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.375 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.377 14469-14469/com.rishav.t2 I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
06-13 04:02:47.647 14469-14469/com.rishav.t2 W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
06-13 04:02:56.393 14469-14469/com.rishav.t2 I/hifiwi: Signup
06-13 04:02:57.126 14469-14469/com.rishav.t2 I/hifiwi: working atleast
06-13 04:02:57.126 14469-14469/com.rishav.t2 I/hifiwi: working atleast
06-13 04:02:57.128 14469-14469/com.rishav.t2 D/AndroidRuntime: Shutting down VM


--------- beginning of crash
06-13 04:02:57.129 14469-14469/com.rishav.t2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rishav.t2, PID: 14469
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.rishav.t2.SigningActivity.deft(SigningActivity.java:208)
at com.rishav.t2.MyDemoListener.onClick(SigningActivity.java:222)
at android.support.design.widget.Snackbar$1.onClick(Snackbar.java:255)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6186)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

Answer Source

Just Define your EditText inside your onCreate and then setText

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText aaaa=(EditText)findViewById(R.id.aaaa);
        aaaa.setText("sss");
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download