Am going through the tutorial in the below link to have a simple Android App with more than one activities.
https://developer.android.com/training/basics/firstapp/starting-activity.html
I have followed the steps as specified in the android tutorial, I run into a NullPointer Exception on the EditText Widget,
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
Below is my MainActivity.java where the exception as editText returns Null though I assume I have wired it in the right way,
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.my.application.message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent = new Intent( this , DisplayMessageActivity.class);
EditText editText;
editText = (EditText) view.findViewById(R.id.edit_message);
System.out.println("the values is "+ editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE,message);
startActivity(intent);
}
}
And below is the activity_main.xml ,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/do_send"
android:onClick="sendMessage"/>
</LinearLayout>
Any pointers here to get past this ?