一旦用户点击"下订单按钮"我正试图将一些数据从一个活动传递到第二个活动。我希望数据显示在第二个活动上,但是给我零或零值。唯一的解释是,未通过捆绑传递的数据未被设置或未被传递。我创建了一个名为size的全局字符串变量,并使其等于xLarge以尝试查找此bug的位置。当我运行程序时,它在第二个活动中返回一个空值。有谁知道为什么?以下是包含一些照片的代码
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);
}
});
}
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 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>
答案 0 :(得分:1)
为什么在活动2中将此设置为null?
由于关键数据类型不匹配。从第一个Activity发送sizePrice
作为String,但尝试获取double
而不是String。
这样做:
double sizePrice = myBundle.getDouble("size");
String size = myBundle.getString("sizePrice"); // return xLarge
答案 1 :(得分:1)
更改此
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);