嗨我怎么能在android中将字符串转换为bigdecimal.
这是我的第一项活动:
public class VIEwCartActivity extends Activity {String mGrandTotal;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { // Todo auto-generated method stub super.onCreate(savedInstanceState); setContentVIEw(R.layout.vIEwcartactivity); ListVIEw mLstVIEw1 = (ListVIEw) findVIEwByID(R.ID.ListVIEw1); TextVIEw mTxtVIEwGrandTotal = (TextVIEw) findVIEwByID(R.ID.mTxtVIEwGrandTotalValue); button mBtnsubmit = (button) findVIEwByID(R.ID.mBtnsubmit); VIEwCartAdapter mVIEwCartAdpt = new VIEwCartAdapter( VIEwCartActivity.this); mLstVIEw1.setAdapter(mVIEwCartAdpt); if (Constants.mItem_Detail.size() > 0) { Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0) .get(SingleMenuItem.KEY_TOTAL)); for (int i = 1; i < Constants.mItem_Detail.size(); i++) { mGTotal = mGTotal + Double.parseDouble(Constants.mItem_Detail.get(i).get( SingleMenuItem.KEY_TOTAL)); } mGrandTotal = String.valueOf(mGTotal); mTxtVIEwGrandTotal.setText("$" + mGrandTotal); } mBtnsubmit.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub Intent i = new Intent(getApplicationContext(), CustomerLogin.class); i.putExtra("GrandTotal", mGrandTotal); startActivity(i); }
CustomerLogin.java(下一个活动):
String mGrandTotal; /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.customer_login); Bundle b = getIntent().getExtras(); String total = b.getString("GrandTotal"); ----- ------ if(isUserValIDated && isPasswordValIDated) { String s= getIntent().getStringExtra(mGrandTotal); Intent intent = new Intent(CustomerLogin.this,PayPalintegrationActivity.class); intent.putExtra(mGrandTotal, s); startActivity(intent); }
PayPalintegrationActivity.java(第3个活动)如下所示:
public class PayPalintegrationActivity extends Activity implements OnClickListener { String mGrandTotal; private PayPal mPayPal; private Checkoutbutton launchPayPalbutton;@OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_pay_pal_integration); ------- ------ @OverrIDe public voID onClick(VIEw v) { payWithPaypal(); } private PayPalPayment payWithPaypal() { PayPalPayment newPayment = new PayPalPayment(); Intent in = getIntent(); String total = in.getStringExtra(mGrandTotal); BigDecimal sPrice = new BigDecimal(total); newPayment.setSubtotal(sPrice); newPayment.setCurrencyType(Currency.getInstance(Locale.US));
我必须将grandtotal值传递给我的paypalintegration活动.
但我的控制台窗口显示以下错误:
12-21 05:52:09.571: E/AndroIDRuntime(782): FATAL EXCEPTION: main 12-21 05:52:09.571: E/AndroIDRuntime(782): java.lang.NullPointerException 12-21 05:52:09.571: E/AndroIDRuntime(782): at java.math.BigDecimal.<init>(BigDecimal.java:483) 12-21 05:52:09.571: E/AndroIDRuntime(782): at com.ssmobileproductions.catalogue.PayPalintegrationActivity.payWithPaypal(PayPalintegrationActivity.java:74) 12-21 05:52:09.571: E/AndroIDRuntime(782): at com.ssmobileproductions.catalogue.PayPalintegrationActivity.onClick(PayPalintegrationActivity.java:66) 12-21 05:52:09.571: E/AndroIDRuntime(782): at androID.vIEw.VIEw.performClick(VIEw.java:2408)
请帮帮我.我的代码中有错误.
我希望需要o / p是:
mGrandTotal值从我的第一个活动传递到最后一个活动(paypalintegration活动)
mGrandTotal值设置为以下行:newPayment.setSubtotal(sPrice);
我该怎么办.请告诉我
解决方法:
你没有因为将String转换为Bigdecimal而得到错误,因为有相同的Bigdecimal构造函数
BigDecimal(String val)
,由于total具有null值而导致错误,因此无法转换为BigDecimal.你可以这样做
String total="0.0"; if(in.getStringExtra(mGrandTotal)!=null && !in.getStringExtra(mGrandTotal).isEmpty()){ total=in.getStringExtra(mGrandTotal); } BigDecimal sPrice = new BigDecimal(total);
希望这对你有用…… !!!
总结以上是内存溢出为你收集整理的java – 在android中将字符串转换为bigdecimal全部内容,希望文章能够帮你解决java – 在android中将字符串转换为bigdecimal所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)