将以下代码写入本机接口实现的类:
package com.mycompany.myapp;
import com.paypal.android.sdk.payments.PayPalAuthorization;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalFuturePaymentActivity;
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 android.content.Intent;
import android.net.Uri;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.R;
import android.widget.Toast;
import android.*;
import android.widget.Toast;
import com.codename1.impl.android.IntentResultListener;
import com.codename1.impl.android.AndroidNativeUtil;
import com.codename1.impl.android.CodenameOneActivity;
//import com.codename1.impl.android.LifecycleListener;
//import com.codename1.impl.android.OnInitListener;
import java.math.BigDecimal;
public class MyNativeImpl{
// private static final String TAG = "paymentdemoblog";
/**
* - Set to PaymentActivity.ENVIRONMENT_PRODUCTION to move real money.
*
* - Set to PaymentActivity.ENVIRONMENT_SANDBOX to use your test credentials
* from https://developer.paypal.com
*
* - Set to PayPalConfiguration.ENVIRONMENT_NO_NETWORK to kick the tires
* without communicating to PayPal's servers.
*/
// private static final String CONFIG_ENVIRONMENT =
// PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;
// note that these credentials will differ between live & sandbox
// environments.
private static final String CONFIG_CLIENT_ID = "Aeqc2X1rBIEUtDNqsaRNr0h1neFo9QnNmfgmpA3D32uSLaHpGJu9NV1KfMnFmy7O-_hV47I7ST0SXDW2";
private static final int REQUEST_CODE_PAYMENT = 1;
private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
// The following are only used in PayPalFuturePaymentActivity.
.merchantName("Hipster Store")
.merchantPrivacyPolicyUri(
Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(
Uri.parse("https://www.example.com/legal"));
PayPalPayment thingToBuy;
private static Activity activity() {
return com.codename1.impl.android.AndroidNativeUtil.getActivity();
}
public String payPalTest() {
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
public void run() {
Intent intent = new Intent(activity(), PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
activity().startService(intent);
}
});
AndroidNativeUtil.getActivity().runOnUiThread(new Runnable() {
public void run() {
thingToBuy = new PayPalPayment(new BigDecimal("10"), "USD",
"HeadSet", PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(activity(),
PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
activity().startActivityForResult(intent, REQUEST_CODE_PAYMENT);
AndroidNativeUtil.startActivityForResult(intent, new IntentResultListener(){
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm = data
.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
System.out.println(confirm.toString());
System.out.println(confirm.toString());
Toast.makeText(activity(), "Order placed",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
System.out.println("The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
System.out
.println("An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
}
} else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT) {
if (resultCode == Activity.RESULT_OK) {
PayPalAuthorization auth = data
.getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
if (auth != null) {
try {
String authorization_code = auth.getAuthorizationCode();
Log.i("FuturePaymentExample", authorization_code);
sendAuthorizationToServer(auth);
Toast.makeText(activity(),
"Future Payment code received from PayPal",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("FuturePaymentExample",
"an extremely unlikely failure occurred: ", e);
e.printStackTrace();
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("FuturePaymentExample", "The user canceled.");
} else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i("FuturePaymentExample",
"Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
}
}
}
});
}
});
// thingToBuy = new PayPalPayment(new BigDecimal("10"), "USD",
// "HeadSet", PayPalPayment.PAYMENT_INTENT_SALE);
//
// Intent intent = new Intent(activity(),PaymentActivity.class);
//
// intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
// activity().startActivityForResult(intent, REQUEST_CODE_PAYMENT);
//Intent intent=new Intent(Intent.ACTION_VIEW);
//intent.setData(Uri.parse("http://www.javatpoint.com"));
//activity().startActivity(intent);
return "test";
}
private void sendAuthorizationToServer(PayPalAuthorization authorization) {
}
public void onDestroy() {
}
public boolean isSupported() {
return false;
}
}
来自主要课程的电话:
MyNative my = (MyNative)NativeLookup.create(MyNative.class);
if(my!= null){
String aa =my.payPalTest();
System.out.println("result::" + aa);
System.out.println("paypalInt" + my.toString());
}
代码构建成功,但当我尝试从Android手机访问它时,它会出错:
不幸的是已经停止了。如何解决问题
答案 0 :(得分:1)
这意味着您有一个未捕获的异常,用电缆连接您的设备并打开android ddms以查看跟踪和日志