Android 我试图将值从一个页面传递到另一个页面,但它没有';不行?
我想将值从一页传递到另一页我的代码是:Android 我试图将值从一个页面传递到另一个页面,但它没有';不行?,android,android-layout,android-intent,android-widget,Android,Android Layout,Android Intent,Android Widget,我想将值从一页传递到另一页我的代码是: public class main extends Activity implements OnClickListener { private static final String TAG = "AskMeShowMe2"; private String choice = null; private String fname = null; private Menu myMenu = null; final static int START =0;
public class main extends Activity implements OnClickListener {
private static final String TAG = "AskMeShowMe2";
private String choice = null;
private String fname = null;
private Menu myMenu = null;
final static int START =0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"Main - onCreate() ...");
setContentView(R.layout.activity_main);
View promptButton = findViewById(R.id.submit_button);
promptButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.greetingsRadioGroup);
int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
switch (checkedRadioButton) {
case R.id.mrButton :
choice = "Mr.";
break;
case R.id.mrsButton :
choice = "Mrs.";
break;
case R.id.msButton:
choice = "Ms.";
break;
case R.id.drButton:
choice = "Dr.";
break;
}
switch(v.getId()){
case R.id.submit_button:
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
startActivity(j);
fname=String.valueOf(R.id.firstname);
Intent t = new Intent(this, Info.class);
j.putExtra("fname", fname);
startActivity(t);
break;
}
}
以及其他信息。类别:
public class Info extends Activity {
private static final String TAG = "Info";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Intent myIntent = getIntent();
Bundle b = myIntent.getExtras();
String choice = b.getString("choice");
String fname = b.getString("fname");
Log.i(TAG,"selection: " + choice);
TextView textView = (TextView) findViewById(R.id.showmeText);
textView.append(": " + choice);
TextView textView1 = (TextView) findViewById(R.id.fname);
textView1.append(": " + fname);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
}
当我运行此程序时,当我单击“提交”按钮时,我的“信息类”页面不会显示,您能帮助我如何修复它吗?使用
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
fname=String.valueOf(R.id.firstname);
j.putExtra("fname", fname);
startActivity(j);
而不是
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
startActivity(j);
fname=String.valueOf(R.id.firstname);
Intent t = new Intent(this, Info.class);
j.putExtra("fname", fname);
startActivity(t);
由于当前您正在两次调用startActivity,第一次是针对
intent j
,第二次是针对intent t
,因此请删除一次,并将choice和fname放在single intent中,只调用startActivity一次 将开关块更改为以下内容:
switch(v.getId()){
case R.id.submit_button:
Intent j = new Intent(this, Info.class);
j.putExtra("choice", choice);
fname=String.valueOf(R.id.firstname);
j.putExtra("fname", fname);
startActivity(j);
break;
}
由于您要两次启动“活动信息”,所以只将附加内容放在一个意图上,而该意图是在更早的时候启动的