【问题标题】:Intent.putExtras() just return nullIntent.putExtras() 只返回 null
【发布时间】:2021-11-26 07:04:51
【问题描述】:

我正在尝试将 intent.putString 发送到其他类,但它不起作用我不知道为什么

trigger.setOnClickListener( v-> {
    Bundle bundle = new Bundle();
    bundle.putString("params", "okay");
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtras(bundle);

    //finishAffinity();
    startActivityForResult(intent,0);

});

当按钮被点击时,我将 bundle 发送到 mainActivity

我从 mainactivity oncreate 收到消息。

Bundle extras = this.getIntent().getExtras();

       if(extras != null ) {
           String _getData = extras.getString("param1");
           Log.i(TAG, "face detect message " + _getData);
       }

但我无法在 MainActivity 上收到任何消息。 请给我一些答案

【问题讨论】:

  • 您已将密钥设置为参数,但在调用它时您使用的是 param1。

标签: java android android-intent onclick


【解决方案1】:

你应该知道 bundle 使用的是键值对,所以如果你创建

bundle.putString("params", "okay");

键是params,值是okay

要获得值,您应该使用相同的键。

String _getData = extras.getString("params");

【讨论】:

    【解决方案2】:

    您必须更改 Bundle 中的 params 键。如下图

    Bundle extras = this.getIntent().getExtras();
    
    if(extras != null ) {
       String _getData = extras.getString("params");
       Log.i(TAG, "face detect message " + _getData);
    }
    

    【讨论】:

      【解决方案3】:

      将其复制到您的 MainActivity 类中:

      Bundle extras = this.getIntent().getExtras();
      
         if(extras != null ) {
             String _getData = extras.getString("params");
             Log.i(TAG, "face detect message " + _getData);
         }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-19
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多