我有一点问题:我有3个活动。活动ListClass.java
public class ListClass extends Activity {
......
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
onItemClick1(parent, view, position, id);
}
});
}
public void onItemClick1(AdapterView<?> parent, View view, int position,
long id) {
String name = (String) parent.getItemAtPosition(position);
String address = (String) listAddress.get(position);
int status = (int) listStatus.get(position);
double lat = (double) listLat.get(position);
double lng = (double) listLng.get(position);
String serialNumber = (String) listSerialNumber.get(position);
getIntent().putExtra("name", name);
getIntent().putExtra("address", address);
getIntent().putExtra("status", status);
getIntent().putExtra("latitude", lat);
getIntent().putExtra("longitude", lng);
getIntent().putExtra("serialNumber", serialNumber);
startActivity(getIntent());
}
public Intent getIntent() {
Intent intent = new Intent(ListClass.this, ClassB.class);
return intent;
}
正如您所看到的,我在ClassB.java上使用了putExtra值:
public class ClassB extends Activity {
TextView textViewTitle;
TextView textView2;
TextView textViewInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.charge_box);
this.textViewTitle = (TextView) findViewById(R.id.charge_box__textView1);
this.textView2 = (TextView) findViewById(R.id.charge_box__textView2);
this.textViewInfo = (TextView) findViewById(R.id.charge_box__textView3);
getActionBar().setDisplayHomeAsUpEnabled(true);
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
String address = bundle.getString("address");
int status = bundle.getInt("status");
textViewTitle.setText(name);
textViewInfo.setText(address + " " + status);
}
}
太好了!但我需要从另一个MainClass.java活动启动ActivityB。
Intent intent = new Intent(MainActivity.this,
ClassB.class);
startActivity(intent);
问题是,当ClassB使用Bundle bundle = getIntent().getExtras();
时,他找不到“额外”,因为MainClass的Intent与ListClass的Intent不同!我能怎么做??我可以决定使用哪种Intent吗? tahnk你!
编辑: @skygeek好吧我正在研究它,但是如果额外的值改变了怎么能创建一个静态变量呢?
public void onItemClick1(AdapterView<?> parent, View view, int position,
long id) {
String name = (String) parent.getItemAtPosition(position);
String address = (String) listAddress.get(position);
int status = (int) listStatus.get(position);
double lat = (double) listLat.get(position);
double lng = (double) listLng.get(position);
String serialNumber = (String) listSerialNumber.get(position);
Intent intent = new Intent(ListClass.this, ChargeBoxInfoActivity.class);
intent.putExtra("name", name);
intent.putExtra("address", address);
intent.putExtra("status", status);
intent.putExtra("latitude", lat);
intent.putExtra("longitude", lng);
intent.putExtra("serialNumber", serialNumber);
startActivity(intent);
}
答案 0 :(得分:2)
在ClassB的onCreate方法中进行检查
if(getIntent().getExtras() != null)
{
// do your functionality with extras
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
String address = bundle.getString("address");
int status = bundle.getInt("status");
textViewTitle.setText(name);
textViewInfo.setText(address + " " + status);
}
else
{
// do whatever you want to do
}
答案 1 :(得分:0)
在第一次调用活动时创建具有静态字段意图的静态对象(检查其是否为null)。因此,下次从另一个类调用相同的活动时,请使用具有已设置意图的相同静态对象再次调用该活动,因此您将找到这些额外内容!
答案 2 :(得分:0)
这是错误的:
getIntent().putExtra("name", name);
getIntent().putExtra("address", address);
getIntent().putExtra("status", status);
getIntent().putExtra("latitude", lat);
getIntent().putExtra("longitude", lng);
getIntent().putExtra("serialNumber", serialNumber);
startActivity(getIntent());
}
public Intent getIntent() {
Intent intent = new Intent(ListClass.this, ClassB.class);
return intent;
}
在每个函数调用getIntent()之后创建新意图... 试试这个:
Intent intent = new Intent(ListClass.this, ClassB.class);
intent.putExtra("name", name);
intent.putExtra("address", address);
intent.putExtra("status", status);
intent.putExtra("latitude", lat);
intent.putExtra("longitude", lng);
intent.putExtra("serialNumber", serialNumber);
startActivity(intent);
答案 3 :(得分:0)
我理解......试试这个:
try{
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
String address = bundle.getString("address");
int status = bundle.getInt("status");
textViewTitle.setText(name);
textViewInfo.setText(address + " " + status);
}}catch (Exception ignored) {
}