将值通过Bundle传递,并在另一个活动中获取其值
将值通过Bundle传递,并在另一个活动中获取其值
如您所见,我正在通过Bundle传递值。
现在,我想在另一个activity的onCreate()中获得它的值。我尝试获取它的值,但它显示nullpointerexception。
请帮我解决这个问题。
Bundle bundle = new Bundle(); String url = "http://www.google.com"; bundle.putString("url", url); Intent myIntent = new Intent(context, NotificationService.class); myIntent.putExtras(bundle); context.startService(myIntent);
获取值的代码:
if (!getIntent().getExtras().getString("url").contains(null)) { // Do something }
admin 更改状态以发布 2023年5月21日
嗨,我希望这段代码能够帮到你。
Bundle bundle = new Bundle(); bundle.putString("name", "Android"); bundle.putString("iname", "iPhone"); Intent intent = new Intent(getApplicationContext(), MyActivity.class); intent.putExtras(bundle); startActivity(intent);
在MyActivity类中
public Bundle getBundle = null; getBundle = this.getIntent().getExtras(); String name = getBundle.getString("name"); String id = getBundle.getString("iname");