将值通过Bundle传递,并在另一个活动中获取其值

16 浏览
0 Comments

将值通过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日
0
0 Comments

嗨,我希望这段代码能够帮到你。

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");

0
0 Comments

应该按照以下步骤进行。

创建一个带有bundle的新Intent并启动Activity。

Intent i = new Intent(context, ActivityName.class);
i.putExtra("key", mystring);
startActivity(i);

在新Activity中的onCreate中像这样获取bundle。

Bundle extras = getIntent().getExtras();
String value;
if (extras != null) {
  value = extras.getString("key");
}

0