Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将类中的数据传递给另一个成员?_Java_Android_Paypal_This - Fatal编程技术网

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

Java 如何将类中的数据传递给另一个成员?,java,android,paypal,this,Java,Android,Paypal,This,我有此代码,我正在尝试将此传递给方法。此成本传递给public void click()方法,但它似乎不起作用,因为我在单击paypal按钮时收到“Paymnet Failed” 我做错了什么 顺便问一下,你怎么称呼private{},private void{}——所有那些看起来像函数()的东西 private void initUI(int theprice) { launchPayPalButton = mPayPal.getCheckoutButton(this,

我有此代码,我正在尝试将此传递给
方法。此成本
传递给
public void click()
方法,但它似乎不起作用,因为我在单击paypal按钮时收到“Paymnet Failed”

我做错了什么

顺便问一下,你怎么称呼
private{}
private void{}
——所有那些看起来像函数()的东西

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;

        }
编辑:我在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);
    }

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

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

String errorMessage = data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
您可以使用调试器对其进行检查,或者使用Logger类中的Log.e函数将其记录到Logcat以便读取

关于你的第二个问题:

顺便问一下,你怎么称呼private{},private void{}——所有那些看起来像函数()的东西

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;

        }
在Java中,那些“看起来像函数的东西”称为方法

编辑:好的,现在您已经向我们展示了您的onCreate方法,我可以看到您从何处获得最终传递给onInit的值:

eprice = myBundle.getInt("eprice");
这样做意味着您以前将值保存在捆绑包中的
onSaveInstanceState(捆绑包)


是你干的吗?当您第一次开始活动时,它是如何获得值的?

使用我最新的代码,我不知道我的eprice为什么不工作。但我有其他的解决办法。我所需要做的就是改变

initUI(eprice)


initUI(Double.valueOf(epricetxt))

你确定它失败是因为成本而不是其他原因吗?errorMessage中有什么?似乎“成本”字段没有问题。您是否检查了在onActivityResult()中收到的“errorID”和“errorMessage”?嗯。。。我检查了一下,但没有错误。但我想知道为什么当我把publicsvoid放在click(View v){payWithPaypal(10);}上时,它会起作用。但是当我把这个放进去的时候,它没有work@user2041851您从哪里调用initUI(inttheprice)?您需要确保调用该方法以将成本设置为有效的整数。我刚刚更新了答案以包含此内容。您好,我现在更清楚地看到错误。传递给click方法的值为“0.0”。我不确定是什么原因导致itOkay,所以你必须弄清楚这是从哪里来的。您将在onCreate中从bundle中获取值,但不清楚该值放在bundle的何处。我用更多信息再次编辑了我的答案。是的。我也这么做了。这是正确的。TextView价格=(TextView)findViewById(R.id.priceTxt);theprice.setText(“价格:$”+epricetxt);此代码显示值已正确保存。但我有其他的解决办法。我所需要做的就是更改initUI(eprice);到initUI(Double.valueOf(epricetxt));它是有效的。嗯,但还是想知道为什么另一个不起作用