electo electo - 23 days ago 6
Android Question

Android app crashes on setText()

I am creating an Android app but i have a crash on a setText()

this is my java :

EditText eText;
TextView chat;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hitch);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

chat = (TextView) findViewById(R.id.chat);
eText = (EditText) findViewById(R.id.edittext);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
chat.setText(eText.getText().toString());
}
});
}


and here is the xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/content_hitch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.mohamad.hitch.Hitch"
tools:showIn="@layout/activity_hitch">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editText"
android:layout_marginBottom="21dp"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/edittext" />

<TextView
android:text="Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/chat" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/input"
android:elevation="0dp"
android:layout_marginEnd="13dp"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentEnd="true" />

</RelativeLayout>


and here is the error :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohamad.hitch, PID: 2415
java.lang.NullPointerException: Attempt to invoke virtualm ethod 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.mohamad.hitch.Hitch$1.onClick(Hitch.java:41)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)


I saw that a lot of other people had the same problem but when i tried each solution none of them worked.

Could you help me please ?

Answer

your EditText id is editText, not edittext. Case sensitive !

change to :

eText = (EditText) findViewById(R.id.editText);