Android 正在检索新活动的多个信息

Android 正在检索新活动的多个信息,android,android-intent,android-activity,Android,Android Intent,Android Activity,我是Android开发编程的新手。我试图将多个信息从开始的活动传递到新的活动,但我不知道如何检索所有的值 我的主要活动按钮发送事件代码如下: public void sendInformation (View view) { Intent intent = new Intent(this, DisplayInformation.class); EditText toText = (EditText) findViewById (R.id.toText);

我是Android开发编程的新手。我试图将多个信息从开始的活动传递到新的活动,但我不知道如何检索所有的值

我的主要活动
按钮发送
事件代码如下:

public void sendInformation (View view) {

    Intent intent = new Intent(this, DisplayInformation.class);
    EditText toText = (EditText) findViewById (R.id.toText);                //Finds the control 'toText' in the form and gets it by ID
    EditText fromText = (EditText) findViewById (R.id.fromText);            //Finds the control 'fromText' in the form and gets it by ID
    EditText messageText = (EditText) findViewById (R.id.messageText);      //Finds the control 'messageText' in the form and gets it by ID
    EditText subjectText = (EditText) findViewById (R.id.subjectText);      //Finds the control 'subjectText' in the form and gets it by ID

    String toMessage = toText.getText().toString();                         //Gets the text that you enter for the 'To'Field
    String fromMessage = fromText.getText().toString();                     //Gets the text that you enter for the 'From' field
    String messageMessage = messageText.getText().toString();               //Gets the text that you enter for the 'Message' field
    String subjectMessage = subjectText.getText().toString();               //Gets the text that you enter for the 'Subject' field

    intent.putExtra(EXTRA_MESSAGE, toMessage);
    intent.putExtra(EXTRA_MESSAGE, fromMessage);
    intent.putExtra(EXTRA_MESSAGE, messageMessage);
    intent.putExtra(EXTRA_MESSAGE, subjectMessage);

    startActivity(intent);
}
EXTRA_MESSAGE
定义为
public final静态字符串EXTRA_MESSAGE=“com.example.Fun.MESSAGE”老实说,我真的不知道这是做什么的,教程刚刚告诉我要包括它

在我的第二个活动页面中,我为
onCreate
实例提供了以下代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_information);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    Intent intent = getIntent();
    String toMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    String fromMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    String subjectMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    String messageMessage = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    TextView toMessagetextView = new TextView(this);
    toMessagetextView.setTextSize(40);
    toMessagetextView.setText(toMessage);

    TextView fromMessagetextView = new TextView(this);
    fromMessagetextView.setTextSize(40);
    fromMessagetextView.setText(fromMessage);

    TextView subjectMessagetextView = new TextView(this);
    subjectMessagetextView.setTextSize(40);
    subjectMessagetextView.setText(subjectMessage);

    TextView messageMessagetextView = new TextView(this);
    messageMessagetextView.setTextSize(40);
    messageMessagetextView.setText(messageMessage);



    setContentView(toMessagetextView);
    setContentView(fromMessagetextView);
    setContentView(subjectMessagetextView);
    setContentView(messageMessagetextView);



}
当我运行我的应用程序时,它会出错并关闭

*另外,如果有人能告诉我如何将
TextView
放入
fragment.xml
文件中进行第二项活动,我将不胜感激。我知道如何声明
id
以及其他什么,但我还不知道如何将其用于从另一个活动检索数据


提前感谢您的帮助。

对于所有额外值,您使用的是相同的键
EXTRA\u MESSAGE
。Intent像Map一样存储值

因此,这里您失去了除
subjectMessage
之外的所有值,因为它是最后添加的

必须为每个值使用不同的键。像

public final static String EXTRA_MESSAGE_TO = "com.example.Fun.MESSAGE_TO";
public final static String EXTRA_MESSAGE_FROM = "com.example.Fun.MESSAGE_FROM";
public final static String EXTRA_MESSAGE_SUBJECT = "com.example.Fun.MESSAGE_SUBJECT";
public final static String EXTRA_MESSAGE = "com.example.Fun.MESSAGE";

因此,我将用另一个名称声明另一个
全局变量
,并将其分配给我的4条
消息之一
?是的。更新答案看一遍好的谢谢你,那么我更新的方法现在更正确了吗?@bbesse谢谢:)