Android 通过bundle传递值并在另一个活动上获取其值

Android 通过bundle传递值并在另一个活动上获取其值,android,android-intent,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, Notificat

正如您在我的代码中看到的,我正在通过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);

获取值代码:

if (!getIntent().getExtras().getString("url").contains(null)) {
        // Do something
}

您必须检查空值

这应该是程序

使用bundle创建新的意图并启动活动

Intent i = new Intent(context, ActivityName.class);
i.putExtra("key", mystring);
startActivity(i);
在onCreate内的新活动中使用如下捆绑包

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

 String url = "http://www.google.com";

Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras("url", url);
startActivity(myIntent);
在另一项活动中,你可以这样做

Bundle extra = getIntent().getExtras();
if(extra != null) {
String value = extra.getString("url")
//use this value where ever you want

  }
替换
startService(…)
->
startActivity(…)

并且还替换

if (getIntent().getExtras().getString("url").contains(null)) {
        // retrieve the url
}


嗨,我希望这个代码能帮助你

Bundle bundle = new Bundle();
bundle.putString("name", "Android");
bundle.putString("iname", "iPhone");
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
intent.putExtras(bundle);
startActivity(intent);
在我的课堂上

public Bundle getBundle = null;
getBundle = this.getIntent().getExtras();
String name = getBundle.getString("name");
String id = getBundle.getString("iname");

因为您将字符串放在一个包中

Bundle youtbundle = new Bundle();
String url = "http://www.google.com";
yourbundle.putString("url", url);
if (!yourbundle.getString("url").contains(null)) {
     // Do something
}
然后将该捆绑包作为附加内容放入目的中

Intent myIntent = new Intent(context, NotificationService.class);
myIntent.putExtras(yourbundle);
context.startService(myIntent);
在开始时,您必须从附加组件中剥离捆绑包

Bundle yourbundle = getIntent().getExtras().getBundle("yourbundle") ;
然后从bundle中获取字符串

Bundle youtbundle = new Bundle();
String url = "http://www.google.com";
yourbundle.putString("url", url);
if (!yourbundle.getString("url").contains(null)) {
     // Do something
}

字符串值=bundle.getString(“请求”)

  • Intent serviceentent=newintent(YourService.class.getName()); serviceIntent.putExtra(“url”,“www.google.com”); 上下文。startService(serviceIntent)

  • 当服务启动时,将调用其onStartCommand()方法,因此在该方法中,您可以从intent对象获取值(url)

  • 公共int onStartCommand(Intent Intent、int标志、int startId){

  • 字符串userID=intent.getStringExtra(“url”)

    返回开始时间; }


    //像这样把价值放在意图中

        Intent in = new Intent(MainActivity.this, Booked.class);
        in.putExtra("filter", "Booked");
        startActivity(in);
    
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String filter = bundle.getString("filter");
    
    //像这样从bundle中获取值

        Intent in = new Intent(MainActivity.this, Booked.class);
        in.putExtra("filter", "Booked");
        startActivity(in);
    
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String filter = bundle.getString("filter");
    

    尝试将此添加到
    通知服务中。类

    String url = getIntent().getStringExtra("url");
    

    . 选中此更改ActivityName.class而不是class,它的.class