我是Android开发编程的新手。我正试图将我的开始活动中的多个信息传递给新活动,我无法弄清楚如何检索所有值。
我的MainActivity buttonSend
事件代码是:
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 static String 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
以及我不知道如何使用它来检索来自其他活动的数据。
提前感谢您的帮助。
答案 0 :(得分:1)
您对所有额外值使用相同的键EXTRA_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";