我正在尝试在应用中实现PayPal功能。为了让它正常工作,我正在尝试创建一个只显示PayPal按钮的简单应用程序,按下该按钮后,会将所有必要参数传递给库,以便用户完成登录和付款流程。
当我运行此代码时,收到以下错误消息:
03-11 20:48:05.588 23566-23566 / org.kevinbright.android.paypaldemo E / AndroidRuntime:FATAL EXCEPTION:main 过程:org.kevinbright.android.paypaldemo,PID:23566 java.lang.RuntimeException:无法启动活动ComponentInfo {org.kevinbright.android.paypaldemo / org.kevinbright.android.paypaldemo.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.RelativeLayout。 addView(android.view.View)'在空对象引用
上
错误与本节有关:
((RelativeLayout) findViewById(R.id.RelativeLayout01)).
addView(launchPayPalButton);
以下是代码:
package org.kevinbright.android.paypaldemo;
public class MainActivity extends Activity {
private CheckoutButton launchPayPalButton;
final static public int PAYPAL_BUTTON_ID = 10001;
private static final int REQUEST_PAYPAL_CHECKOUT = 2;
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);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.bottomMargin = 10;
// 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(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
payPalButtonClick(arg0);
}
});
// Add the listener to the layout
launchPayPalButton.setLayoutParams(params);
launchPayPalButton.setId(PAYPAL_BUTTON_ID);
((RelativeLayout) findViewById(R.id.RelativeLayout01)).
addView(launchPayPalButton);
}
public void payPalButtonClick(View arg0) {
PayPalPayment newPayment = new PayPalPayment();
newPayment.setSubtotal(new BigDecimal(_theSubtotal));
newPayment.setCurrencyType("USD");
newPayment.setRecipient("my@email.com");
newPayment.setMerchantName("My Company");
Intent checkoutIntent = PayPal.getInstance().checkout(newPayment, this);
startActivityForResult(checkoutIntent, REQUEST_PAYPAL_CHECKOUT);
}
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);
}
}
}
我不明白为什么我收到此错误消息,因为launchPayPalButton
引用了pp
,PayPal Class
是在initLibrary
中初始化的showPayPalButton
的对象调用 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>
</RelativeLayout>
之前的方法。
请帮助!!!
编辑:这与引用的问题不重复。我已经查看了该帖子,该帖子是关于引用尚未初始化的对象,因此返回空引用。这个问题解释了为什么这不应该生成空指针异常,因为该对象已初始化。
编辑2:这是名为“customize.xml”的.xml文件,保存在“Res。”下的Layouts文件夹中。
{{1}}