2016-03-11 129 views
0

我正在使用PayPal的應用程序。我需要使用MPL,而不是SDK,因爲我的應用需要能夠實施第三方支付。我已經關注了各種教程並創建了下面的代碼。我沒有收到任何編譯器錯誤,也沒有登錄錯誤,但是當我運行它並點擊「Pay with PayPal」按鈕時,沒有任何反應。相反,當我點擊按鈕或屏幕上的任何位置時,我會得到ViewPostImeInputStage ACTION_DOWNonClick沒有做任何事情...「ViewPostImeInputStage ACTION_DOWN」

我不知道爲什麼。請幫忙!

public class MainActivity extends Activity implements View.OnClickListener { 

private CheckoutButton launchPayPalButton; 
final static public int PAYPAL_BUTTON_ID = 10001; 
private double _theSubtotal; 
private double _taxAmount; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    initLibrary(); 
    showPayPalButton(); 
} 

private void showPayPalButton() { 
    LinearLayout linearLayout = new LinearLayout(this); 
    linearLayout.setOrientation(LinearLayout.VERTICAL); 

    ViewGroup.LayoutParams linearLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 

    setContentView(linearLayout, linearLayoutParam); 

    LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

    // Generate the PayPal checkout button and save it for later use 
    PayPal pp = PayPal.getInstance(); 
    launchPayPalButton = pp.getCheckoutButton(this, PayPal.BUTTON_194x37, CheckoutButton.TEXT_PAY); 

    // The OnClick listener for the checkout button 
    launchPayPalButton.setOnClickListener(this); 

    // Add the listener to the layout 
    launchPayPalButton.setLayoutParams(lpView); 
    launchPayPalButton.setId(PAYPAL_BUTTON_ID); 
    linearLayout.addView(launchPayPalButton); 

    } 

public void PayPalButtonClick(View arg0) { 
    PayPalPayment newPayment = new PayPalPayment(); 
    newPayment.setSubtotal(new BigDecimal(_theSubtotal)); 
    newPayment.setCurrencyType("USD"); 
    newPayment.setRecipient("[email protected]"); 
    newPayment.setMerchantName("My Company"); 
    Intent paypalIntent = PayPal.getInstance().checkout(newPayment, this); 
    this.startActivityForResult(paypalIntent, 2); 

} 



public void initLibrary() { 
    PayPal pp = PayPal.getInstance(); 

    if (pp == null) { // Test to see if the library is already initialized 

     // This main initialization call takes your Context, AppID, and target server 
     pp = PayPal.initWithAppID(this, "APP-80W284485P519543T", PayPal.ENV_NONE); 

     // Required settings: 

     // Set the language for the library 
     pp.setLanguage("en_US"); 

     // Some Optional settings: 

     // Sets who pays any transaction fees. Possible values are: 
     // FEEPAYER_SENDER, FEEPAYER_PRIMARYRECEIVER, FEEPAYER_EACHRECEIVER, and FEEPAYER_SECONDARYONLY 
     pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER); 

     // true = transaction requires shipping 
     pp.setShippingEnabled(false); 


    } 

} 

@Override 
public void onClick(View arg0){ 

    PayPalButtonClick(arg0); 
} 

}

回答

0

可能是因爲你沒有設置super.oncreate(之後的內容視圖)和因爲你在點擊監聽器註冊您的活動的,其響應點擊從任何地方在屏幕而不只是按鈕。

編輯

上點擊監聽器添加到按鈕這樣

paypalButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View arg0) { PayPalButtonClick(arg0); } 
}); 

,並從活動中刪除OnClickListener實施

+0

謝謝...我有問題,因爲我從來沒有設置意見程序;只使用了.xml文件。我應該使用'setContentView(R.layout.activity_main);'?如何解決onClickListener問題? –

+0

看起來你不是從xml構建你的視圖,所以在你的showPaypalButton()的末尾做setContentView(linearLayout)應該可以工作。 – monopandora

+0

我把它改爲: 'launchPayPalButton.setOnClickListener(新View.OnClickListener(){ @覆蓋 公共無效的onClick(查看爲arg0){ payPalButtonClick(爲arg0);} });' 但問題遺蹟。另外,我在showPaypalButton方法中設置了contentView。 –

0

ViewPostImeInputStage ACTION_DOWN基本上是一個條件時,你的佈局被拒絕並且您不再能夠點擊任何可點擊的項目。解決方案很簡單,只需將您的佈局內容與父項一起打包即可。

爲前:

if you have the xml with format as: 
<LinearLayout <---root layout 
..... contents here 
</LinearLayout> <-- root layout end 

change to 

<FrameLayout <---root layout 
    <LinearLayout <-- parent wrap start 
    ... 
<!-- your content --> 
    </LinearLayout> <-- parent wrap end 
</FrameLayout> <-- root layout end 

獲取更多信息,您可以考慮瓦納閱讀this

+0

謝謝,但我以編程方式創建佈局,所以我沒有使用.xml文件。 –

+0

爲什麼不使用xml解決你的問題? –

+0

因爲我使用PayPal MPL中的按鈕代碼,所以我不認爲我可以。 –