在 Bundle 中传递多个值,最后一个字符串中分配的值被传递给所有字符串

Passing Multiple value in Bundle, value assigned in last String is getting passed to all string

我正在尝试通过 Bundle 将 9 个字符串从一个 java class 传递到另一个 class。但是在另一个屏幕中,分配给最后一个额外内容的值被所有其他字符串覆盖。

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);

}

}

在 class2 的末尾,我放置了日志,发现所有字符串都填充了分配给 QUANTITY 的值。

请澄清以上内容。如果您需要更多代码,将共享相同的代码。

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'

只需使用 唯一 值初始化您用于将项目添加到 Bundle 中的所有上述 public static Strings,否则它们将相互覆盖。例如

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";

此外,理想情况下它们应该是 public static final String。请考虑也使用一致的命名约定 (TOTAL_CHARGE、TURN_OVER)。

你可以把Bundle想象成一个Map,如果你用相同的键放置值,值将被覆盖。这就是为什么你得到所有键的 QUANTITY 值的原因,因为所有键都是空的(相等的)并且 QUANTITY 是你放入 Bundle.[=28 的最后一个键=]

编辑: 对于 NullPointerException.

在 Class 1 (HomeScreen) 中,在 calculateProfit(View view) 方法中,您将 Bundle 放入 Intent 中,无需使用密钥。

intent.putExtras(extras);

然后在 Class 2 (displayProfit) inside onCreate(Bundle savedInstanceState) 你得到 Bundle from the Intent with a key.

intent.getBundleExtra(HomeScreen.x);

所以像这样在 HomeScreen 中使用相同的键放置 extras

intent.putExtra(HomeScreen.x, extras);