获取代码为:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
if(bundle != null){
phoneNumber = bundle.getString(ParameterContant.PHONENUMBER);
etx_phoneNumber.setText(phoneNumber);
}
设置代码为:
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle bundle = new Bundle();
bundle.putString(ParameterContant.PHONENUMBER, phoneNumber);
ActivityUtil.changeActivity(LoginActivity.this, RegistActivity.class, false, bundle);
}
ActivityUtil.changeActivity方法为:
public static void changeActivity(final Activity context,
final Class<?> clazz, final boolean exit, Bundle bundle) {
Intent intent = new Intent(context, clazz);
context.startActivity(intent);
if (bundle != null) {
intent.putExtras(bundle);
}
if (exit) {
context.finish();
}
}
问题是我这边在changeActivity()方法中设断点都能看到bundle里面的值。。为什么在获取的时候就为null了?不解呀~~
------解决方案--------------------
用bundle传对象,你的取的方法不对吧?
首先,对象要serializable的,其次,取的时候,要getIntent().getSerializableExtra(object);
如果你仅仅要传String,那传的时候,intent.putExtra("obj",strObj);
取的时候:intent=this.getIntent(); String obj=intent.getStringExtra("obj");
------解决方案--------------------
public static void changeActivity(final Activity context,
final Class<?> clazz, final boolean exit, Bundle bundle) {
Intent intent = new Intent(context, clazz);
if (bundle != null) {
intent.putExtras(bundle);
}
context.startActivity(intent);
if (exit) {
context.finish();
}
}
------解决方案--------------------
context.startActivity(intent);
放在intent.putExtras(bundle);后面试试
------解决方案--------------------
Intent.putExtras的说明:
Add a set of extended data to the intent. The keys must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
Key需要包含package作为前缀