如何将类中的数据传递给另一个成员?

时间:2013-02-13 02:41:58

标签: java android paypal this

我有这个代码,我正在尝试将this.thecost传递给public void click()方法,但看起来它不起作用,因为我在点击paypal按钮时收到“Paymnet Failed”。

我出了什么问题?

顺便说一句,你称之为private {}private void {} - 所有那些function()看起来的东西?

private void initUI(int theprice) {

        launchPayPalButton = mPayPal.getCheckoutButton(this, 
                PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);

        LinearLayout.LayoutParams params = new
                LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                        LayoutParams.WRAP_CONTENT);

        params.bottomMargin = theprice;
        this.thecost = theprice;

        launchPayPalButton.setLayoutParams(params);
        launchPayPalButton.setOnClickListener(this);

        ((LinearLayout)findViewById(R.id.main_layout2)).addView(launchPayPalButton);
    }

    public void onClick(View v) {
        payWithPaypal(this.thecost);
    }


private void payWithPaypal(Integer gg) {
        PayPalPayment newPayment = new PayPalPayment();
        BigDecimal bigDecimal=new BigDecimal(gg);
        newPayment.setSubtotal(bigDecimal);
        newPayment.setCurrencyType(Currency.getInstance(Locale.US));
        newPayment.setRecipient("email@hotmail.com");
        newPayment.setMerchantName("My Merchant");
        Intent paypalIntent = PayPal.getInstance().checkout(newPayment, this);
        this.startActivityForResult(paypalIntent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(resultCode) {
        case Activity.RESULT_OK:
            String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
            data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
            Toast.makeText(this,"Paymnet Successful",Toast.LENGTH_LONG).show();
            break;
        case Activity.RESULT_CANCELED:
            Toast.makeText(this,"Paymnet Cancel",Toast.LENGTH_LONG).show();
            break;
        case PayPalActivity.RESULT_FAILURE:
            Toast.makeText(this,"Paymnet Failed",Toast.LENGTH_LONG).show();
            String errorID = 
            data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
            String errorMessage = 
                    data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
            break;

        }

EDITED:我在oncreate方法

上调用initUI()

再次编辑:我将全局变量更改为'double',因为价格通常有小数位。 现在我试图干杯,我看到错误更加清晰。 toast显示一条消息,表明传递的值为“0.0”。因此,存在“付款失败”和付款无效的错误。

int eprice;
    double thecost;    
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);

    //Data mSource = new Data;
    Intent myLocalIntent = getIntent();
    Bundle myBundle = myLocalIntent.getExtras();



    eprice = myBundle.getInt("eprice");
    String epricetxt = myBundle.getString("eprice");

    Adapter mAdapter = new Adapter(this, mSource);
    //details = (Data) mAdapter.getItem(pos);





    TextView theprice = (TextView) findViewById(R.id.priceTxt);
    theprice.setText("Price: $" + epricetxt);





    this.setCost(eprice);
    this.thecost = eprice;



    //Paypal
    mPayPal=PayPal.initWithAppID(this,Constants.PAYPAL_APP_ID,PayPal.ENV_SANDBOX);

    initUI(eprice);

}

private void initUI(int theprice) {

        launchPayPalButton = mPayPal.getCheckoutButton(this, 
                PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);

        LinearLayout.LayoutParams params = new
                LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                        LayoutParams.WRAP_CONTENT);

        params.bottomMargin = theprice;
        this.thecost = theprice;
        launchPayPalButton.setLayoutParams(params);
        launchPayPalButton.setOnClickListener(this);

        ((LinearLayout)findViewById(R.id.main_layout2)).addView(launchPayPalButton);
    }

    public void onClick(View v) {
        //
        payWithPaypal(getCost());
    }

    public void setCost(int cost) {
        this.thecost = cost;
    }

    public double getCost() {
        return this.thecost;
    }


    private void payWithPaypal(Double gg) {
        PayPalPayment newPayment = new PayPalPayment();
        Toast.makeText(getApplicationContext(),gg.toString(), Toast.LENGTH_LONG).show();
        BigDecimal bigDecimal=new BigDecimal(gg);
        newPayment.setSubtotal(bigDecimal);
        newPayment.setCurrencyType(Currency.getInstance(Locale.US));
        newPayment.setRecipient("email@hotmail.com");
        newPayment.setMerchantName("My Merchant");
        Intent paypalIntent = PayPal.getInstance().checkout(newPayment, this);
        this.startActivityForResult(paypalIntent, 1);
    }

2 个答案:

答案 0 :(得分:0)

我们没有足够的信息告诉您这里出了什么问题。出于某种原因,PayPal API返回了失败状态代码。也许你没有互联网连接?

检查您在此行中检索到的错误消息字符串的值:

String errorMessage = data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);

您可以使用调试器对其进行检查,或使用Logger类中的Log.e函数将其记录到Logcat,以便您可以阅读它。

至于你的第二个问题:

  

顺便说一句,你称之为私有{},私有空{} - 所有那些function()看东西?

在Java中,那些“功能查找”被称为方法。

编辑:好的,既然你已经向我们展示了你的onCreate方法,我可以看到你在哪里获得最终传递给onInit的值:

eprice = myBundle.getInt("eprice");

执行此操作意味着您已保存先前在onSaveInstanceState(Bundle)

中的捆绑中的值 你做到了吗?当你第一次启动Activity时它是如何获得值的?

答案 1 :(得分:0)

使用我最新的代码,我不知道为什么我的eprice不起作用。但我有替代解决方案。我需要做的就是改变

initUI(EPRICE);

initUI(Double.valueOf(epricetxt));