在Android中,如何使用Intent在活动之间传递值?
我想将一个活动类中职位的值传递给另一个 我的代码如下:在Android中,如何使用Intent在活动之间传递值?,android,Android,我想将一个活动类中职位的值传递给另一个 我的代码如下: protected void onListItemClick(ListView listView, View v, int position, long id) { switch (position) { case 1: Intent newActivity1 = new Intent(this, BucketItemActivity.class
protected void onListItemClick(ListView listView, View v, int position,
long id) {
switch (position) {
case 1:
Intent newActivity1 = new Intent(this, BucketItemActivity.class);
newActivity1.putExtra("bucketno", position);
startActivity(newActivity1);
break;
case 2:
Intent newActivity2 = new Intent(this, BucketItemActivity.class);
newActivity2.putExtra("bucketno", position);
startActivity(newActivity2);
break;
}
}
将接收它的活动类
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bucket_item);
String value = getIntent().getExtras().getString("bucketno");
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(value);
setContentView(textView);
}
但我总是得到一个空的字符串值
请帮忙。更换这个
String value = getIntent().getExtras().getString("bucketno");
与
您正在尝试传递int值,但正在检索字符串数据。这就是您获得nullpointerException的原因。您可以使用:
在第一个活动中(主活动页面)
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
然后,您可以通过以下方式从第二个活动中获得它:在第二个活动中(第二个活动页面)
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
使用:
getIntent().getExtras()
将为您提供边界get()
方法可用于获取值。此外,谁有不同的位置
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = intent.getExtras().getString("key").toString();
}
double userDMH = Util.getInstance(TakeInfoOne.this).calculateBMH(userGender, kg, talll, currentAge, activityy);
Intent intentTogeneral = new Intent(TakeInfoOne.this, TakeInfoTwo.class);
intentTogeneral.putExtra("user_current_age",userDMH );
startActivity(intentTogeneral);
如果放置其他原语,如double、boolean、int,请不要忘记在secont活动中获取值时键入正确的格式
Bundle extras = getIntent().getExtras();
if (extras != null) {
double value = extras.getDouble("user_current_age");
txt_metabolism.setText(""+value);
}
在这里使用extras.getDouble()
键入相关的值类型,如
extras.getInt("user_current_age");
extras.getBoolean("user_current_age");
extras.getString("user_current_age");
从第一个活动开始
Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
i.putExtra("prac","test");
startActivity(i);
String prac=getIntent().getStringExtra("prac);
在第二幕活动中获得价值
Intent i=new Intent(getApplicationContext(),BookPractionerAppoinment.class);
i.putExtra("prac","test");
startActivity(i);
String prac=getIntent().getStringExtra("prac);
对于序列化对象:
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
通过
得到
它应该是
int value
或String value=String.valueOf(…)代码>。您正试图使用此位代码将int
存储在字符串中@Andro Selva是对的,但他必须得到parseException或classcaste异常,但他得到的是nullpointer异常?我以前没有想到的另一件事--setText(value)
将在int
上失败(在此上下文中)String.valueOf()
是更好的方法。如果需要字符串本身,甚至可以使用phil的解决方案。两者都可以正常工作。受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_bucket__item);Bundle Bundle=getIntent().getExtras();String value=Bundle.getString(“bucketno”);TextView TextView=(TextView)findviewbyd(R.layout.textView1);textView.setTextSize(40);textView.setText(值);}