为什么我的数据包没有发送到我的另一个 activity?

Why is my bundle of data not being sent to my other activity?

我正在尝试在用户单击 "Place your order button" 后将一些数据从一个 activity 传递到第二个 activity。我希望数据显示在第二个 activity 上,但它给了我空值或零值。唯一的解释是通过捆绑包传递的数据没有被设置,或者没有被传递。我制作了一个名为 size 的全局字符串变量,并使其等于 xLarge 以尝试查找此错误所在的位置。当我 运行 程序时,它 returns 第二个 activity 中的空值。有谁知道为什么?这是带有一些照片的代码

Activity 1

    package com.example.switchviews;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Activity1 extends Activity {
    /** Called when the activity is first created. */
    EditText mEdit;
    EditText mEdit2;
    double amount;
    double small;
    double meduium;
    double large;
    int count = 0;
    int numToppings;
    double toppingsTot=0;
    Button next;
    String size ="xLarge";
    double sizePrice;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
         next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                    Intent myIntent = new Intent(Activity1.this, Activity2.class);
                    Bundle myData = new Bundle();
                    myData.putDouble("price", amount);
                    myData.putDouble("toppings", toppingsTot);
                    myData.putInt("numberOfToppings", numToppings);
                    myData.putDouble("size", sizePrice);
                // Why is this set to null in activity 2?
                    myData.putString("sizePrice", size);
                    int[] myLittleArray = {1, 2, 3, 4,5,6, 7};
                    myData.putIntArray("myIntArray1", myLittleArray);
                    myIntent.putExtras(myData);
                    startActivityForResult(myIntent,2);
                }




        });
    }

Activity 2

    package com.example.switchviews;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Activity2 extends Activity {

    TextView pizza;
    TextView toppings;
    TextView total;
    Button submit;
    Button cancel;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        pizza = (TextView)findViewById(R.id.pizza);
        toppings = (TextView) findViewById(R.id.toppings);
        total = (TextView)findViewById(R.id.total);
        submit = (Button)findViewById(R.id.Button02);
        cancel = (Button)findViewById(R.id.Button03);

        Intent myLocalIntent = getIntent();
        Bundle myBundle = myLocalIntent.getExtras();

        double amount = myBundle.getDouble("price");
        double toppingsTot = myBundle.getDouble("toppings");
        int numToppings = myBundle.getInt("numberOfToppings");
        double sizePrice = myBundle.getDouble("sizePrice");
        String size = myBundle.getString("size");
        int[] arr1 = myBundle.getIntArray("myIntArray1");

        pizza.setText("Pizza "+ size + " 1x" + Double.toString(sizePrice)+ "    "+ "   "+ Double.toString(sizePrice) );


        Button next = (Button) findViewById(R.id.Button03);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }

}

清单XML

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.switchviews"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.switchviews.Activity1"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity2"></activity>
    </application>

</manifest>

为什么在 activity 2 中将此设置为空?

由于关键数据类型不匹配。从第一个 Activity 发送 sizePrice 作为字符串,但试图获取 double 而不是字符串。 这样做:

double sizePrice = myBundle.getDouble("size");
String size = myBundle.getString("sizePrice"); // return xLarge

改变这个

    myData.putDouble("size", sizePrice); // Why is this set to null in activity 2?
 myData.putString("sizePrice", size);

    myData.putDouble("sizePrice", sizePrice); 
 myData.putString("size", size);