PayPal.initWithAppID()与NullPointerException崩溃

时间:2013-03-20 09:33:29

标签: android paypal crash nullpointerexception unity3d

 FATAL EXCEPTION: Thread-5195
 java.lang.NullPointerException: src == null
    at java.lang.System.arraycopy(Native Method)
    at com.paypal.android.a.g.a(Unknown Source)
    at com.paypal.android.a.h.c(Unknown Source)
    at com.paypal.android.MEP.PayPal.setLanguage(Unknown Source)
    at com.paypal.android.MEP.PayPal.initWithAppID(Unknown Source)
    at com.lonfun.pets.UnityPayMenuActivity.initLibrary(UnityPayMenuActivity.java:256)
    at com.lonfun.pets.UnityPayMenuActivity.access$2(UnityPayMenuActivity.java:249)
    at com.lonfun.pets.UnityPayMenuActivity$2$1.run(UnityPayMenuActivity.java:175)

我只是用android来打开paypal,一切都还可以,但如果我从团结中打电话,就会发生这种情况! 这是我的java代码:

包com.paypal.androidpets;

import android.content.Context;
import android.os.Handler;

import com.lonfun.pets.UnityPayMenuActivity;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalActivity;

public class PaypalTool {
    Context context;
    PayPal pp;
    private static final int server = PayPal.ENV_SANDBOX;
    // The ID of your application that you received from PayPal
    private static final String appID = "APP-80W284485P519543T";

    public PaypalTool(Context context) {
        this.context=context;
    }

    public void initLibrary(final Handler handler) 
    {
        new Thread() {
            public void run() {
                PayPalActivity._paypal = null;
                pp = PayPal.getInstance();
                // If the library is already initialized, then we don't need to initialize it again.
                if(pp == null) {
//                  pp = PayPal.initWithAppID(context, "", PayPal.ENV_NONE);
                    pp = PayPal.initWithAppID(context, appID, server);

                    pp.setShippingEnabled(false);
                }

                if (PayPal.getInstance().isLibraryInitialized()) {
                    handler.sendEmptyMessage(0);
                }
                else {
                    handler.sendEmptyMessage(1);
                }
            }
        }.start();
    }
}

有人解决了这个问题,请帮帮我,谢谢!

using UnityEngine;
using System.Collections;

public class PayMoneyHandler : MonoBehaviour
{

    #if UNITY_ANDROID
    static AndroidJavaClass payPluginClass;
    static AndroidJavaClass unityPlayer;
    static AndroidJavaObject currActivity;
    #endif

    public static void Pay(string productID)
    {
#if UNITY_ANDROID
        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo.Call("StartActivity0", 
            "com.peten.tier2", 
            "EGAHCNJDEJBWBIBWEXIKDEGLJECOAD.Mac", 
            "Dog", 
            "AndDogCn", 
            "http://42.121.94.123/TradeWeb/AlipayTrade.aspx",
            "http://58.246.188.238/TradeWeb/PaypalTrade.aspx"
            );
#endif
    }

}
using UnityEngine;
using System.Collections;

public class TestForPaypal : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void OnClick(){
        Debug.Log("Test For Paypal!");
        PayMoneyHandler.Pay("test1");
    }
}

public void StartActivity0(final String name,final String PlayerName,final String GameID,final String ZoneID,final String payUrl,final String paypalUrl) 
    {       
        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Log.i("Order", "name:"+name+"PlayerName:"+PlayerName+"GameID:"+GameID+"ZoneID:"+ZoneID+"payUrl:"+payUrl+"paypalUrl:"+paypalUrl);
                Intent payintent=new Intent(mContext,UnityPayMenuActivity.class);
                payintent.putExtra("name",name);
                payintent.putExtra("PlayerName",PlayerName);
                payintent.putExtra("GameID",GameID);
                payintent.putExtra("ZoneID",ZoneID);
                payintent.putExtra("payUrl",payUrl);
                payintent.putExtra("paypalUrl",paypalUrl);
                Unity3DMainActiviy.this.startActivity(payintent);
            }
        });
    }

3 个答案:

答案 0 :(得分:0)

我认为您没有输入appID和服务器详细信息。 下面是我实现的工作代码,它对我来说很好。 我实现了按钮点击按钮,这扩展了可运行的

 Check_out.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

             if (_paypalLibraryInit) {
         showPayPalButton();
         //Toast.makeText(Android_PP_Test1Activity.this, captions[position], Toast.LENGTH_SHORT).show();

         }
             else {
             // Display a progress dialog to the user and start checking for when
         // the initialization is completed


             Thread initThread = new Thread(initLibraryRunnable);
             initThread.start();

             _progressDialog = new ProgressDialog(Recharge.this);
         _progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
         _progressDialog.setMessage("Loading PayPal Payment Library");
         _progressDialog.setCancelable(false);
         _progressDialog.show();
         _progressDialogRunning = true;
         Thread newThread = new Thread(checkforPayPalInitRunnable);

         newThread.start();
         //Toast.makeText(Android_PP_Test1Activity.this, "Picasso Paintings", Toast.LENGTH_SHORT).show();


        }
    }


}); 

此按钮调用此runnable方法

//This lets us run the initLibrary function
    final Runnable initLibraryRunnable = new Runnable() {
        public void run() {
            initLibrary();
        }
    };

并且此方法负责启动initLibrary。

 /** init method **/
public void initLibrary() {
    PayPal pp = PayPal.getInstance();
    if (pp == null) {
        // This is the main initialization call that takes in your Context,
        // the Application ID, and the server you would like to connect to.
        pp=PayPal.initWithAppID(this,"APP80W284485P519543T",PayPal.ENV_SANDBOX);

        // -- These are required settings.
        pp.setLanguage("en_US"); // Sets the language for the library.
        // --

        // -- These are a few of the optional settings.
        // Sets the fees payer. If there are fees for the transaction, this
        // person will pay for them. Possible values are FEEPAYER_SENDER,
        // FEEPAYER_PRIMARYRECEIVER, FEEPAYER_EACHRECEIVER, and
        // FEEPAYER_SECONDARYONLY.
        pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);
        // Set to true if the transaction will require shipping.
        pp.setShippingEnabled(true);
        // Dynamic Amount Calculation allows you to set tax and shipping
        // amounts based on the user's shipping address. Shipping must be
        // enabled for Dynamic Amount Calculation. This also requires you to
        // create a class that implements PaymentAdjuster and Serializable.
        pp.setDynamicAmountCalculationEnabled(false);
        // -- 


    }
}

希望它有所帮助......

答案 1 :(得分:0)

问题是当unity生成apk文件时。我比较了一个我犯了错误的apk和PayPal集成只用eclipse编译和其他用Unity编译。

让我们说我只用eclipse做的apk叫做“PayPalOnlyEclipse.apk”,用统一的“PayPalWithUnity.apk”做成的; y将它们重命名为“PayPalOnlyEclipse.zip”和“PayPalWithUnity.zip”,然后用WinRar打开它们进行比较。

我发现PayPalOnlyEclipse有一个PayPalWithUnity没有的文件夹,文件夹是“com \ paypal \ android \ utils \ data”。所以我解压缩了该文件夹,然后将其添加到PayPalWithUnity。然后擦除META-INF文件夹(签名),然后关闭“PayPalWithUnity.zip”并将其重命名为之前的状态(“PayPalWithUnity.apk”)。

然后我用命令签名apk文件: jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore my.keystore PayPalWithUnity.apk用户

最后我使用了zipalign的命令: zipalign -v 4 PayPalWithUnity.apk PayPalWithUnity2.apk

该命令会生成PayPalWithUnity2.apk,这是您必须在手机中安装的apk文件。

希望它有所帮助,对不起我的英语。

答案 2 :(得分:0)

检查您是否在清单文件中包含了PaypalActivity。像这样:

<activity android:name="com.paypal.android.MEP.PayPalActivity"
        android:configChanges="keyboardHidden|orientation" /> 

如果你有它并且它还没有工作,请在构建路径中包含库(属性&gt;&gt; Java Buid路径&gt;&gt;库&gt;&gt;添加JAR)并将其添加到“订单和导出”选项卡上(属性&gt;&gt; Java Buid路径&gt;&gt;订单和导出)。