I'm trying to pass 9 string from one java class to another class via Bundle. But in the another screen, value assigned to last extra is overwritten to all other string.
Class1
public class HomeScreen extends AppCompatActivity {
public static String Buy_Price;
public static String Sell_Price;
public static String QUANTITY;
public static String BROKERAGE;
public static String ActualProfitLoss_String;
public static String TurnOver_String;
public static String STT_String;
public static String ServiceTax_String;
public static String TotalCharge_String;
public static String x;
private static String Strin ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
}
public void calculateProfit(View view) {
Intent intent = new Intent(this, displayProfit.class);
Bundle extras =new Bundle();
EditText editText=(EditText) findViewById(R.id.editText);
EditText editText4=(EditText) findViewById(R.id.editText4);
EditText editText2=(EditText) findViewById(R.id.editText2);
EditText editText3=(EditText) findViewById(R.id.editText3);
String buyPriceString = editText3.getText().toString();
Float BuyPrice = Float.parseFloat(buyPriceString);
String quantityString = editText2.getText().toString();
Float Quantity = Float.parseFloat(quantityString);
String sellPriceString = editText4.getText().toString();
Float SellPrice = Float.parseFloat(sellPriceString);
String brokerageString = editText.getText().toString();
Float Brokerage = Float.parseFloat(brokerageString);
double TurnOver=(BuyPrice*Quantity)+(SellPrice*Quantity);
double Sell1 =SellPrice-(SellPrice*(Brokerage/100));
double Buy1 = BuyPrice+(BuyPrice*(Brokerage/100));
double TotalBrokerage = ((Sell1*.03)+(Buy1*0.3));
double STT = (Quantity*(SellPrice-(SellPrice*(Brokerage/100))))*(.025/100);
double TrnxChrge = TurnOver*(0.00275/100);
double ServiceTax = (TotalBrokerage+STT)*(15/100);
double SEBICharge = (TurnOver*(.0002/100));
double TotalCharge = ServiceTax+TrnxChrge+SEBICharge+STT;
double NetProfit = (Sell1-Buy1)*Quantity;
double ActualProfitLoss = (NetProfit -TotalCharge);
String ActualProfitLossString=Double.toString(ActualProfitLoss);
String TurnOverString=Double.toString(TurnOver);
String STTString=Double.toString(STT);
String ServiceTaxString=Double.toString(ServiceTax);
String TotalChargeString=Double.toString(TotalCharge);
extras.putString(ActualProfitLoss_String,ActualProfitLossString);
extras.putString(TurnOver_String,TurnOverString);
extras.putString(STT_String,STTString);
extras.putString(ServiceTax_String,ServiceTaxString);
extras.putString(TotalCharge_String,TotalChargeString);
extras.putString(Buy_Price,buyPriceString);
extras.putString(Sell_Price,sellPriceString);
extras.putString(BROKERAGE,brokerageString);
extras.putString(QUANTITY,quantityString);
intent.putExtras(extras);
startActivity(intent);
}
Class 2:
public class displayProfit extends AppCompatActivity {
private static String Strin ;
protected void onCreate(Bundle savedInstanceState) {
Log.d(Strin, "buyPrice :::");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_profit);
Intent intent =getIntent();
Bundle extras = intent.getBundleExtra(HomeScreen.x);
String Buy_Price = extras.getString(HomeScreen.Buy_Price);
String Sell_Price = extras.getString(HomeScreen.Sell_Price);
String QUANTITY = extras.getString(HomeScreen.QUANTITY);
String BROKERAGE = extras.getString(HomeScreen.BROKERAGE);
String ActualProfitLoss_String = extras.getString(HomeScreen.ActualProfitLoss_String);
String TurnOver_String = extras.getString(HomeScreen.TurnOver_String);
String STT_String = extras.getString(HomeScreen.STT_String);
String ServiceTax_String = extras.getString(HomeScreen.ServiceTax_String);
String TotalCharge_String = extras.getString(HomeScreen.TotalCharge_String);
TextView Buy_Price1= (TextView) findViewById(R.id.Buy_Price1);
TextView Sell_Price1= (TextView) findViewById(R.id.Sell_Price1);
TextView QUANTITY1= (TextView) findViewById(R.id.QUANTITY1);
TextView BROKERAGE1= (TextView) findViewById(R.id.BROKERAGE1);
TextView ActualProfitLoss_String1= (TextView) findViewById(R.id.ActualProfitLoss_String1);
TextView TurnOver_String1= (TextView) findViewById(R.id.TurnOver_String1);
TextView STT_String1= (TextView) findViewById(R.id.STT_String1);
TextView ServiceTax_String1= (TextView) findViewById(R.id.ServiceTax_String1);
TextView TotalCharge_String1= (TextView) findViewById(R.id.TotalCharge_String1);
Buy_Price1.setText(Buy_Price);
Sell_Price1.setText(Sell_Price);
QUANTITY1.setText(QUANTITY);
BROKERAGE1.setText(BROKERAGE);
ActualProfitLoss_String1.setText(ActualProfitLoss_String);
TurnOver_String1.setText(TurnOver_String);
STT_String1.setText(STT_String);
ServiceTax_String1.setText(ServiceTax_String);
TotalCharge_String1.setText(TotalCharge_String);
}
}
In the end of class2 I placed log and found that Value assigned to QUANTITY is getting populated for all the string.
Please clarify the above. If you more code needed will share the same.
Logs for the same after changing the Class1 as suggested in answer
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection W/IInputConnectionWrapper: finishComposingText on inactive InputConnection W/IInputConnectionWrapper: finishComposingText on inactive InputConnection I/art: Background sticky concurrent mark sweep GC freed 25273(2MB) AllocSpace objects, 15(264KB) LOS objects, 44% free, 4MB/8MB, paused 14.345ms total 168.562ms
[ 02-20 23:21:14.614 3314: 3314 D/ ]
buyPrice :::
[ 02-20 23:21:14.861 3314: 3314 D/ ] NotNull:::::: D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.test.tax, PID: 3314 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.tax/com.test.tax.displayProfit}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.test.tax.displayProfit.onCreate(displayProfit.java:29) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Disconnected from the target VM, address: 'localhost:8625', transport: 'socket'
Just initialize all the above public static String
s which you are using to add items into the Bundle
with unique values else they will override each other. eg.
public static String Buy_Price = "a";
public static String Sell_Price = "b";
public static String QUANTITY = "c";
public static String BROKERAGE = "d";
public static String ActualProfitLoss_String = "e";
public static String TurnOver_String = "f";
public static String STT_String = "g";
public static String ServiceTax_String = "h";
public static String TotalCharge_String = "i";
public static String x = = "j";
Also, they should ideally be public static final String
. Please consider using a consistent naming convention too (TOTAL_CHARGE, TURN_OVER).
You can imagine the Bundle
as a Map, if you put values with the same key, the values will be overridden. That is why you got the value of QUANTITY
in for all the keys because all the keys were null (equal) and QUANTITY
was the last key you put in the Bundle
.
EDIT:
For the NullPointerException
.
In Class 1 (HomeScreen) inside calculateProfit(View view)
method you put the Bundle
in the Intent
without a key.
intent.putExtras(extras);
Then in Class 2 (displayProfit) inside onCreate(Bundle savedInstanceState)
you get the Bundle
from the Intent
with a key.
intent.getBundleExtra(HomeScreen.x);
So put the extras using the same key in HomeScreen like so
intent.putExtra(HomeScreen.x, extras);