我正在尝试通过Bundle将9个字符串从一个java类传递到另一个类。但是在另一个屏幕中,分配给最后一个额外值的值将被覆盖到所有其他字符串。
的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);
}
第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的最后,我放置了log,发现分配给QUANTITY的Value正在为所有字符串填充。
请澄清以上内容。如果您需要更多代码,则会共享相同内容。
按照回答
中的建议更改Class1后记录相同内容 W / IInputConnectionWrapper:在非活动的InputConnection上的finishComposingText W / IInputConnectionWrapper:在非活动的InputConnection上的finishComposingText W / IInputConnectionWrapper:在非活动的InputConnection上的finishComposingText 我/艺术:背景粘性并发标记扫描GC释放25273(2MB)AllocSpace对象,15(264KB)LOS对象,44%免费,4MB / 8MB,暂停14.345ms总计168.562ms[ 02-20 23:21:14.614 3314: 3314 D/ ]
buyPrice :::
[02-20 23:21:14.861 3314:3314 D /] NOTNULL :::::: D / AndroidRuntime:关闭VM
--------- beginning of crash
E / AndroidRuntime:致命异常:主要 处理:com.test.tax,PID:3314 java.lang.RuntimeException:无法启动活动ComponentInfo {com.test.tax/com.test.tax.displayProfit}:java.lang.NullPointerException:尝试调用虚方法'java.lang.String android.os.Bundle。 null对象引用上的getString(java.lang.String)' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.os.Bundle.getString(java.lang.String)' 在com.test.tax.displayProfit.onCreate(displayProfit.java:29) 在android.app.Activity.performCreate(Activity.java:6662) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 断开与目标VM的连接,地址:'localhost:8625',transport:'socket'
答案 0 :(得分:1)
只需初始化您用于将项目添加到具有唯一值的public static String
的所有上述Bundle
,否则它们将相互覆盖。例如
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
想象为地图,如果您使用相同的键放置值,则会覆盖这些值。这就是为什么你为所有键获得了QUANTITY
的值的原因,因为所有的键都是null(相等)而QUANTITY
是你放在Bundle
中的最后一个键。
编辑:
对于NullPointerException
。
在{1}}方法中的第1类(HomeScreen)中,您将calculateProfit(View view)
放入Bundle
而没有按键。
Intent
然后在intent.putExtras(extras);
内的第2课(displayProfit)中,您可以使用密钥从onCreate(Bundle savedInstanceState)
获取Bundle
。
Intent
所以在HomeScreen中使用相同的密钥来添加额外内容,如此
intent.getBundleExtra(HomeScreen.x);