4

嗨,我如何在 android 中将字符串转换为 bigdecimal。

这是我的第一个活动:

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(下一个活动):

      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(第三个活动)看起来像:

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

我必须将总价值传递给我的 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)

请帮助我。我的代码有什么问题。

I wish to need the o/p是:

mGrandTotal 值是从我的第一个活动传递到最后一个活动(paypalintegration 活动)

mGrandTotal 值设置为以下行: newPayment.setSubtotal(sPrice);

我该怎么办。请告诉我

4

2 回答 2

7

您不会因为将 String 转换为 Bigdecimal 而出现错误,因为对于相同的 BigDecimal(String val)有一个 Bigdecimal 的构造函数, 由于total具有值并且无法转换为 BigDecimal,您会收到错误。你可以这样做

      String total="0.0";
      if(in.getStringExtra(mGrandTotal)!=null && !in.getStringExtra(mGrandTotal).isEmpty()){
            total=in.getStringExtra(mGrandTotal);
      }
       BigDecimal sPrice = new BigDecimal(total);

希望这对你有用……!!!

于 2012-12-21T06:40:01.863 回答
1

您收到错误是因为您试图将非数字字符串转换total为 BigDecimal 数字。将一些数值分配给KEY_TOTAL例如

          static final String KEY_TOTAL = "0.0";//some numeric value here
于 2012-12-21T05:15:08.267 回答