Jee*_*mar 13 android android-intent
正如我在代码中看到的那样,我正在通过Bundle传递价值.
现在,我希望它在另一项活动中具有价值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);
Run Code Online (Sandbox Code Playgroud)
获取价值代码:
if (!getIntent().getExtras().getString("url").contains(null)) {
// Do something
}
Run Code Online (Sandbox Code Playgroud)
Sre*_*v R 26
这应该是程序.
使用bundle创建一个新的Intent并启动该活动.
Intent i = new Intent(context, ActivityName.class);
i.putExtra("key", mystring);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)
在onCreate中的新Activity中取这样的包
Bundle extras = getIntent().getExtras();
String value;
if (extras != null) {
value = extras.getString("key");
}
Run Code Online (Sandbox Code Playgroud)
Beb*_*T.N 24
嗨,我希望这段代码可以帮到你.
Bundle bundle = new Bundle();
bundle.putString("name", "Android");
bundle.putString("iname", "iPhone");
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
intent.putExtras(bundle);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
在MyActivity.class中
public Bundle getBundle = null;
getBundle = this.getIntent().getExtras();
String name = getBundle.getString("name");
String id = getBundle.getString("iname");
Run Code Online (Sandbox Code Playgroud)
if (getIntent().getExtras().getString("url") != null) {
// retrieve the url
}
Run Code Online (Sandbox Code Playgroud)
你必须检查空值