我正在将Paypal集成到我的Android应用程序中,现在它一直在崩溃,甚至无法启动。我曾经堆栈跟踪找到我的错误,这是我得到的错误,不幸的是,我仍然不确定如何去解决它。
使用:转换后的Eclipse文件到Android Studio
01-24 16:33:56.581 1581-1581/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service com.paypal.android.sdk.payments.PayPalService@b219deb0 with Intent { cmp=com.app.myapp/com.paypal.android.sdk.payments.PayPalService (has extras) }: java.lang.RuntimeException: Service extras invalid. Please check the docs.
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2507)
at android.app.ActivityThread.access$1900(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Service extras invalid. Please check the docs.
at com.paypal.android.sdk.payments.PayPalService.a(Unknown Source)
at com.paypal.android.sdk.payments.PayPalService.onStartCommand(Unknown Source)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2490)
at android.app.ActivityThread.access$1900(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
编辑(包括演员):
package com.app.myapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;
import org.apache.cordova.CordovaActivity;
import java.math.BigDecimal;
public class MainActivity extends CordovaActivity
{
TextView m_response;
PayPalConfiguration m_configuration;
// the id is the link to the paypal account, we have to create an app and get its id
String m_paypalClientId = "AUa...yYPeJiy\n";
Intent m_service;
int m_paypalRequestCode = 999; // or any number you want
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_response = (TextView) findViewById(R.id.response);
m_configuration = new PayPalConfiguration()
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX) // sandbox for test, production for real
.clientId(m_paypalClientId);
m_service = new Intent(this, PayPalService.class);
Intent intent = m_service.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, m_configuration);// configuration above
startService(m_service); // paypal service, listening to calls to paypal app
}
void pay(View view)
{
PayPalPayment payment = new PayPalPayment(new BigDecimal(10), "SGD", "Test payment with Paypal",
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(this, PaymentActivity.class); // it's not paypalpayment, it's paymentactivity !
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, m_configuration);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, m_paypalRequestCode);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == m_paypalRequestCode)
{
if(resultCode == Activity.RESULT_OK)
{
// we have to confirm that the payment worked to avoid fraud
PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if(confirmation != null)
{
String state = confirmation.getProofOfPayment().getState();
if(state.equals("approved")) // if the payment worked, the state equals approved
m_response.setText("payment approved");
else
m_response.setText("error in the payment");
}
else
m_response.setText("confirmation is null");
}
}
}
}