Reputation: 35
I am trying to pass some data from one activity to the second activity once the user clicks on the "Place your order button". I want the data to show up on the second activity but is is giving me either null or zero values. Only explanation is that the data being passed through the bundle is not being set, or is not being delivered. I made a global string variable called size and made it equal to xLarge to try and find where this bug is. When I run the program, it returns a null value in the second activity. Does anyone know why? Here is the code with some photos
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>
Upvotes: 0
Views: 90
Reputation: 12388
Change this
myData.putDouble("size", sizePrice); // Why is this set to null in activity 2?
myData.putString("sizePrice", size);
To
myData.putDouble("sizePrice", sizePrice);
myData.putString("size", size);
Upvotes: 1
Reputation: 132992
Why is this set to null in activity 2?
Due to key data-type mismatch. sending sizePrice
as String from first Activity but trying to get as double
instead of String.
Do it as:
double sizePrice = myBundle.getDouble("size");
String size = myBundle.getString("sizePrice"); // return xLarge
Upvotes: 2