【问题标题】:Passing values through bundle and get its value on another activity通过捆绑传递值并在另一个活动中获取其值
【发布时间】:2013-06-10 10:57:31
【问题描述】:

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

【问题讨论】:

标签: android android-intent


【解决方案1】:

这应该是程序。

使用 bundle 创建一个新的 Intent 并启动 Activity。

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

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

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

【讨论】:

  • 更改ActivityName.class 不是Class,它的.class
【解决方案2】:

您好,希望这段代码对您有所帮助。

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.class 中

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

【讨论】:

    【解决方案3】:
    if (getIntent().getExtras().getString("url") != null) {
            // retrieve the url
    }
    

    你必须检查空值

    【讨论】:

      【解决方案4】:

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

          Intent in = new Intent(MainActivity.this, Booked.class);
          in.putExtra("filter", "Booked");
          startActivity(in);
      

      // 像这样从 bundle 中获取价值

          Intent intent = getIntent();
          Bundle bundle = intent.getExtras();
          String filter = bundle.getString("filter");
      

      【讨论】:

        【解决方案5】:

        试试这个

         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
        
          }
        

        【讨论】:

          【解决方案6】:

          因为你把你的字符串放在一个包里

          Bundle youtbundle = new Bundle();
          String url = "http://www.google.com";
          yourbundle.putString("url", url);
          

          然后将捆绑包作为意图的额外内容

          Intent myIntent = new Intent(context, NotificationService.class);
          myIntent.putExtras(yourbundle);
          context.startService(myIntent);
          

          一开始你必须从附加组件中剥离捆绑包

          Bundle yourbundle = getIntent().getExtras().getBundle("yourbundle") ;
          

          然后从 yourbundle 中获取字符串

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

          【讨论】:

            【解决方案7】:

            替换startService(...) - > startActivity(...)

            同时替换

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

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

            【讨论】:

              【解决方案8】:

              字符串值 = bundle.getString("request");

              【讨论】:

                【解决方案9】:
                1. 意图 serviceIntent = new Intent(YourService.class.getName()); serviceIntent.putExtra("url", "www.google.com"); context.startService(serviceIntent);

                  1. 当服务启动时,它的 onStartCommand() 方法会被调用,所以在这个方法中你可以从 Intent 对象中获取值(url)

                  2. public int onStartCommand (Intent intent, int flags, int startId){

                  String userID = intent.getStringExtra("url");

                  返回 START_STICKY; }

                【讨论】:

                  【解决方案10】:

                  尝试在NotificationService.class中添加这个:

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-03-08
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-07-16
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多