0

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)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rishav
  • 3,818
  • 1
  • 31
  • 49
  • By the way, there is an error in your logcat when you do this, and you should include your errors in the question – OneCricketeer Jun 12 '17 at 22:12
  • There weren't any errors while building . I will include the errors of the logcat though. – Rishav Jun 12 '17 at 22:16
  • I mean runtime errors. You said `setText` doesn't work, and your app should have crashed before it ever reached that code – OneCricketeer Jun 12 '17 at 22:17
  • @cricket_007 Thanks for mentioning. Edited. – Rishav Jun 12 '17 at 22:20
  • Thanks, but you should also make sure your code matches the stacktrace. The error is in `SigningActivity`, not `MainActivity` – OneCricketeer Jun 12 '17 at 22:25
  • Ya I know. The error is coming because I am trying to setText from signingActivity using an object of MainActivity. Guess I'll have to ask a new proper questions regarding that. Been Google since 1 hour. I don't know how can I do that. – Rishav Jun 12 '17 at 22:41
  • You shouldn't be "using an object / instance of MainActivity" anywhere. You use Intents to pass data between Activities – OneCricketeer Jun 13 '17 at 00:28
  • Ohhhh is that so. So to setText of some editText from a different class I need to use intents and not objects right? If that's so I'll work on it. Thanks a bunch. – Rishav Jun 13 '17 at 05:42
  • I mean, if I understood correctly, then, yes. https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application – OneCricketeer Jun 13 '17 at 05:51

4 Answers4

3

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");
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Meikiem
  • 1,876
  • 2
  • 11
  • 19
3

You can define a View as a field, but you cannot initialize it there.

Technical reason: There is no Context defined for the Activity at the point of class initialization

public class MainActivity extends AppCompatActivity {

    private EditText aaaa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        aaaa=(EditText)findViewById(R.id.aaaa);
        aaaa.setText("sss");
    }
}

This is also the better approach if you plan on having other methods

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
2

You are trying to access the editText before it have been created on the screen. Move this line

 EditText aaaa=(EditText)findViewById(R.id.aaaa);

After the

setContentView(R.layout.activity_main);
EditText aaaa=(EditText)findViewById(R.id.aaaa);
aaaa.setText("sss");
android_Muncher
  • 1,057
  • 7
  • 14
0

I use on Kotlin next

edit_text_view.text!!.append("some_text")

Did not check, but I think in Java there will be something like that -

edit_text_view.setText(edit_text_view.getText().append("some_text"));

If you want set new text, use clear() function first (for Editable -> edit_text_view.getText())

biruk1230
  • 3,042
  • 4
  • 16
  • 29