Java – convert strings to BigDecimal in Android
•
Java
Hey, how can I convert a string to BigDecimal in Android
This is my first activity:
public class ViewCartActivity extends Activity {
String mGrandTotal;
@Override
protected 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 (next activity):
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 (activity 3) is as follows:
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));
I have to pass the grandtotal value to my payalintegration activity
But my console window displays the following error:
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)
Please help me There is an error in my code
I want o / P to be:
The mgrandtotal value is passed from my first activity to my last activity (payalintegration activity)
Set the mgrandtotal value to the following line: newpayment setSubtotal(sPrice);
What should I do? please tell me
Solution
You don't get an error because you convert string to BigDecimal, because you have the same BigDecimal constructor
String total="0.0";
if(in.getStringExtra(mGrandTotal)!=null && !in.getStringExtra(mGrandTotal).isEmpty()){
total=in.getStringExtra(mGrandTotal);
}
BigDecimal sPrice = new BigDecimal(total);
I hope it works for you!!!
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
