【问题标题】:Problem receiving data from an Intent in Android Studio在 Android Studio 中从 Intent 接收数据时出现问题
【发布时间】:2021-07-13 22:03:55
【问题描述】:

我正在编写通过 Intent 发送和接收数据的代码

但是,代码总是在以下方面存在问题: 总结结果

请问,谁能帮帮我?

发送数据的代码:

    ``` nextPage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
            Bundle sendlerBundler = new Bundle();
            sendlerBlundler.putString("summaryResult", summaryResult);

            senderIntent.putExtras(sendlerBundler);
            startActivity(senderIntent);

            Toast.makeText(getApplicationContext(), "It was sent: " + summaryResult, Toast.LENGTH_LONG).show();
            Intent intent = new Intent(getApplicationContext(), UserRegistration2.class);
            startActivity(intent);

            Bundle param = new Bundle();

            param.putString("name", name);
            param.putString("birthDate", birthDate);
            parame.putString("city", city);
            param.putString("summaryResult", summaryResult);

            senderIntent.putExtras(param);
            startActivity(senderIntent);
            startActivity(intent);
        }
    });``` 

接收数据的代码:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userRegistration);

        Intent receiverIntent = getIntent();
        Bundle receiverBundle = receiverIntent.getExtras();
        String summaryResult =
                receiverBundle.
                        getString
                                **("summaryResult");**
        Toast.makeText(UserRegistration.this, "Receiving the Summary Result: " +summaryResult, Toast.LENGTH_LONG).show();```

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    您声明了 startActivity 4 次,这是错误的,首先您应该将所有想要的数据放入包中,将包添加到意图中,然后只启动一次活动。代码应如下所示:

     public void onClick(View view) {
            Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
            Bundle sendlerBundler = new Bundle();
            sendlerBlundler.putString("summaryResult", summaryResult);
    
            senderIntent.putExtras(sendlerBundler);
            startActivity(senderIntent);
    
        }
    

    接收活动中的代码:

       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userRegistration);
    
        Intent receiverIntent = getIntent();
        Bundle receiverBundle = receiverIntent.getExtras();
        String summaryResult = receiverBundle.getString("summaryResult");
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多