I tried to display User Input from my main activity to my second activity, but when I typed in and click the send button on the main activity the whole app just crashes. Please help!
crash solved! Thanks a lot! However, I can't get the message to display on the second activity.
MainActivity.java
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import static android.R.id.message;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String message = getResources().getString(R.string.UserInput);
final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent sendMessage = new Intent(MainActivity.this, DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
startActivity(sendMessage);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
tools:context="com.example.dell_inspiron.sendmessage.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="@string/edit"
android:ems="10"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp"
android:id="@+id/UserInput" />
<Button
android:text="@string/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/UserInput"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="@+id/send"
android:onClick="sendMessage" />
<TextView
android:text="@string/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
DisplayMessageActivity.java
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent Extra = getIntent();
String textView = Extra.getStringExtra("UserInput");
TextView UserInput = (TextView) findViewById(R.id.UserOutput);
UserInput.setText(textView);
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
launchActivity();
}
});
}
private void launchActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_display_message"
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"
tools:context="com.example.dell_inspiron.sendmessage.DisplayMessageActivity">
<Button
android:text="@string/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:id="@+id/button2" />
<TextView
android:text="@string/UserOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:id="@+id/UserOutput" />
<TextView
android:text="@string/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/enter"
android:textSize="18sp"
android:textColor="@android:color/holo_purple" />
</RelativeLayout>
You mentioned that you are getting the user input from the MainActivity but i don't see anything like that in your code but you can do the below to pass some value from one activity to another...
Change your second activity like this
to
and change your MainActivity like this
to
and
to
There is issue with your second Activity.You are passing int value in Intents but trying to get String value.
Change it as below:
You are passing
int
, but receive it inString
, so your app crashed.MainActivity
DisplayMessageActivity
You should also add
into
launchActivity
method.Solution 1
Change
String textView = Extra.getString("UserInput");
toint textView= Extra.getIntExtra("UserInput", 0);
Solution 2
To send the message to your second Activity, you need to use intent inside your MainActivity button click. Modify your code as below.
Solution 3
This line look wrong to me
If you want to get the
editText
string, this is the way to go