Display User Input from one activity to another in

2019-09-07 02:52发布

问题:

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>

回答1:

You are passing int, but receive it in String, so your app crashed.

MainActivity

 int message = R.string.UserInput;
    Intent sendMessage = new Intent(MainActivity.this, DisplayMessageActivity.class);
    sendMessage.putExtra("UserInput", message);

DisplayMessageActivity

Bundle Extra = getIntent().getExtras();
String textView = Extra.getString("UserInput");

You should also add

intent.putExtra("UserInput", message); 

into launchActivity method.

Solution 1

Change String textView = Extra.getString("UserInput"); to int 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.

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 static android.R.id.message;

public class MainActivity extends AppCompatActivity {

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

    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);
        }
    });
}
}

Solution 3

This line look wrong to me

final String message = getResources().getString(R.string.UserInput);

If you want to get the editText string, this is the way to go

EditText input = (EditText)findViewById(R.id.UserInput);  // declare your editText

final String message = input.getText().toString();  // get your input type


回答2:

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

Bundle Extra = getIntent().getExtras();
String textView = Extra.getString("UserInput");

to

Intent Extra = getIntent();
String textView = Extra.getStringExtra("UserInput");

and change your MainActivity like this

int message = R.string.UserInput;

to

String message= getResources().getString(R.string.UserInput);

and

final Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
      launchActivity();

    }
});

to

Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
      startActivity(sendMessage);

    }
});


回答3:

There is issue with your second Activity.You are passing int value in Intents but trying to get String value.

Change it as below:

 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.TextView;

  public class DisplayMessageActivity extends AppCompatActivity {

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

 Bundle Extra = getIntent().getExtras();
 int textView = Extra.getInt("UserInput");

TextView UserInput = (TextView) findViewById(R.id.UserInput2);
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);
}

}