我想将一个 Activity 类中的位置值传递给另一个...
我的代码如下:
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;
}
}
将接收它的 Activity 类..
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 value = getIntent().getExtras().getInt("bucketno");
您正在尝试传递 int 值但检索字符串数据。这就是您收到 nullpointerException 的原因。
关于android - 如何在 Android 中使用 Intent 在 Activity 之间传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14017720/