I'm trying to implement PayPal functionality in an app. In order to get it working, I'm trying to create a simple app that only shows the PayPal button that, once pressed, will pass all of the necessary parameters to the library to take the user through the log-in and payment process.
When I run this code, I get the following error message:
03-11 20:48:05.588 23566-23566/org.kevinbright.android.paypaldemo E/AndroidRuntime: FATAL EXCEPTION: main Process: org.kevinbright.android.paypaldemo, PID: 23566 java.lang.RuntimeException: Unable to start activity ComponentInfo{org.kevinbright.android.paypaldemo/org.kevinbright.android.paypaldemo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
The error relates to this section:
((RelativeLayout) findViewById(R.id.RelativeLayout01)).
addView(launchPayPalButton);
Here's the code:
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);
}
}
}
I don't understand why I'm getting this error message, since launchPayPalButton
references pp
, which is an object of the PayPal Class
that is initialized in the initLibrary
method before showPayPalButton
is called.
Please help!!!
Edit: This is not a duplicate of the questions referenced. I have reviewed that post and that post is about referening objects that have not been initialized and thus, return a null reference. This question explains why this should not generate a null pointer exception because the object is initialized.
Edit 2: Here's my .xml file called "customize.xml" that saved in the Layouts folder under "Res."
<?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>