【问题标题】:google wallet integration null reference android谷歌钱包集成空参考android
【发布时间】:2013-12-05 06:26:55
【问题描述】:

我正在尝试在我的应用程序中集成谷歌钱包。我已经按照这个教程https://developers.google.com/commerce/wallet/instant-buy/android/tutorial

但我在

处收到空引用错误
mWalletClient = new WalletClient(this, WalletConstants.ENVIRONMENT_SANDBOX,"icube1solutions@gmail.com" ,
                WalletConstants.THEME_HOLO_LIGHT, connectionCallbackListener , connectionFailedListener); 

由于connectionFailedListener和connectionCallbackListener函数。

这是我尝试过的以下代码:

  import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
  import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
    import com.google.android.gms.wallet.Address;
    import com.google.android.gms.wallet.Cart;
    import com.google.android.gms.wallet.FullWallet;
    import com.google.android.gms.wallet.FullWalletRequest;
    import com.google.android.gms.wallet.LineItem;
    import com.google.android.gms.wallet.MaskedWallet;
    import com.google.android.gms.wallet.MaskedWalletRequest;
    import com.google.android.gms.wallet.NotifyTransactionStatusRequest;
    import com.google.android.gms.wallet.WalletClient;
    import com.google.android.gms.wallet.WalletConstants;

    public class Upgrade extends Activity {
        static final int REQUEST_CODE_RESOLVE_PRE_AUTH = 1010;
        static final int REQUEST_CODE_RESOLVE_LOAD_MASKED_WALLET = 1001;
        static final int REQUEST_CODE_RESOLVE_CHANGE_MASKED_WALLET = 1002;
        static final int REQUEST_CODE_LOAD_MASKED_WALLET=1000;
        static final int REQUEST_CODE_RESOLVE_LOAD_FULL_WALLET = 1004;
        private static final String googleTransactionId = null;


          MaskedWallet mMaskedWallet;
          FullWallet mFullWallet;
        WalletClient mWalletClient;
        NotifyTransactionStatusRequest notifyTxRequest;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Set View to register.xml
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.upgrade);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.profile_custom_titlebar);
            ConnectionCallbacks connectionCallbackListener = null;
            OnConnectionFailedListener connectionFailedListener = null;
            mWalletClient = new WalletClient(this, WalletConstants.ENVIRONMENT_SANDBOX,"icube1solutions@gmail.com" ,
                    WalletConstants.THEME_HOLO_LIGHT, connectionCallbackListener , connectionFailedListener);
            Button btnCompletePurchase=(Button)findViewById(R.id.btnCompletePurchase);
            btnCompletePurchase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    completepurchase();
                     notifyTxRequest = NotifyTransactionStatusRequest.newBuilder()
                              .setGoogleTransactionId(mFullWallet.getGoogleTransactionId())
                              .setStatus(NotifyTransactionStatusRequest.Status.SUCCESS)
                              .build();

                }
            });
            displayListView();
        }
        @Override
        public void onStart() {
            super.onStart();
            mWalletClient.connect();
        }

        public void connectionCallbackListener()
        {
            Log.d("success","success");
        }
        public void connectionFailedListener()
        {

            Log.d("error","error");
        }
        public void completepurchase()
        {
            mWalletClient.loadMaskedWallet(maskedWalletRequest,
                    REQUEST_CODE_LOAD_MASKED_WALLET);


            mWalletClient.notifyTransactionStatus(notifyTxRequest);

            mWalletClient.changeMaskedWallet(mFullWallet.getGoogleTransactionId(),mFullWallet.getMerchantTransactionId(),REQUEST_CODE_RESOLVE_CHANGE_MASKED_WALLET);
            mWalletClient.loadFullWallet(fullWalletRequest,REQUEST_CODE_RESOLVE_LOAD_FULL_WALLET);

            String accountNumber  =  mFullWallet.getProxyCard().getPan();
            String securityCvv  = mFullWallet.getProxyCard().getCvn();
            //int expirationYear = mFullWallet.getExpirationYear();
            //int expirationMonth = mFullWallet.getExpirationMonth();
            Address billingAddress = mFullWallet.getBillingAddress();
            Address shippingAddress = mFullWallet.getShippingAddress();







        }



        FullWalletRequest fullWalletRequest = FullWalletRequest.newBuilder()
                  //.setGoogleTransactionId( mMaskedWallet.getGoogleTransactionId())
                .setGoogleTransactionId(googleTransactionId)
                  .setCart(Cart.newBuilder()
                          .setCurrencyCode("USD")
                         .setTotalPrice("10.00")
                          .addLineItem(LineItem.newBuilder()
                                  .setCurrencyCode("USD")
                                  .setDescription("Premium Membership")
                                  .setQuantity("1")
                                 .setUnitPrice("10")
                              .setTotalPrice("10.00")
                                  .build())
                          .build())
                  .build();


        MaskedWalletRequest maskedWalletRequest =
                  MaskedWalletRequest.newBuilder()
                  //.setMerchantName(Constants.MERCHANT_NAME)
                  .setMerchantName("Udebate")
                  .setPhoneNumberRequired(true)
                  .setShippingAddressRequired(true)
                  .setCurrencyCode("USD")
                  .setCart(Cart.newBuilder()
                     // .setCurrencyCode(Constants.CURRENCY_CODE_USD)
                          .setCurrencyCode("USD")
                      .setTotalPrice("10.00")
                      .addLineItem(LineItem.newBuilder()
                              .setCurrencyCode("USD")
                              .setDescription("Premium Membership")
                              .setQuantity("1")
                              .setUnitPrice("10")
                              .setTotalPrice("10.00")
                              .build())

                      .build())
                  .setEstimatedTotalPrice("10.00")
                  .build();

                @Override
                public void onStop() {
                  super.onStop();
                  mWalletClient.disconnect();
                }




        public void onConnected(Bundle connectionHint) {
            mWalletClient.checkForPreAuthorization(
                REQUEST_CODE_RESOLVE_PRE_AUTH);
        }

        public void onActivityResult(int requestCode, int resultCode,
                Intent data) {

            switch (requestCode) {
                case REQUEST_CODE_RESOLVE_PRE_AUTH:
                    switch (resultCode) {
                        case Activity.RESULT_OK:
                            boolean mIsPreAuthed = data.getBooleanExtra(

                                    WalletConstants.EXTRA_IS_USER_PREAUTHORIZED, false);

                            break;
                        case Activity.RESULT_CANCELED:
                            break;
                        case REQUEST_CODE_RESOLVE_LOAD_MASKED_WALLET:
                          switch (resultCode) {
                              case Activity.RESULT_OK:
                                   mMaskedWallet =
                                          data.getParcelableExtra(WalletConstants.EXTRA_MASKED_WALLET);
                                  launchConfirmationPage(mMaskedWallet);
                                  break;
                              case Activity.RESULT_CANCELED:
                                  // The user cancelled the flow
                                  break;
                              default:
                                  // Handle the error
                                  int errorCode =
                                      data.getIntExtra(
                                          WalletConstants.EXTRA_ERROR_CODE, -1);
                                  handleError(errorCode);
                                  break;
                          }
                          break;
                        case REQUEST_CODE_RESOLVE_CHANGE_MASKED_WALLET:
                            switch (resultCode) {
                                case Activity.RESULT_OK:
                                    mMaskedWallet =
                                    data.getParcelableExtra(
                                        WalletConstants.EXTRA_MASKED_WALLET);
                                    // Update UI with new Masked Wallet
                                    break;
                                case Activity.RESULT_CANCELED:
                                    // nothing to do here
                                    break;
                                default:
                                    int errorCode =
                                        data.getIntExtra(
                                            WalletConstants.EXTRA_ERROR_CODE, -1);
                                        handleError(errorCode);
                                        break;
                            }
                            break;
                        case REQUEST_CODE_RESOLVE_LOAD_FULL_WALLET:
                            switch (resultCode) {
                                case Activity.RESULT_OK:
                                    mFullWallet  =
                                    data.getParcelableExtra(
                                        WalletConstants.EXTRA_FULL_WALLET);
                                    // the full wallet can now be used to
                                    // process the customer's payment. Send the
                                    // wallet info up to server to process, and to
                                    // get the result for sending
                                    // transaction status
                                    break;
                                case Activity.RESULT_CANCELED:
                                    // nothing to do here
                                    break;
                                default:
                                    int errorCode =
                                        data.getIntExtra(
                                            WalletConstants.EXTRA_ERROR_CODE, -1);
                                    handleError(errorCode);
                                    break;
                            }
                            break;

                        default:
                            break;
                    }

            }
        }





        private void launchConfirmationPage(MaskedWallet mMaskedWallet2) {
            // TODO Auto-generated method stub
            Log.d("confirmed","confirmed");

        }






        void handleError(int errorCode) {
            switch (errorCode) {
                case WalletConstants.ERROR_CODE_SPENDING_LIMIT_EXCEEDED:
                    Toast.makeText(this,"Spending limit exceeded",
                            Toast.LENGTH_LONG).show();
                    break;
                case WalletConstants.ERROR_CODE_INVALID_PARAMETERS:
                case WalletConstants.ERROR_CODE_AUTHENTICATION_FAILURE:
                case WalletConstants.ERROR_CODE_BUYER_ACCOUNT_ERROR:
                case WalletConstants.ERROR_CODE_MERCHANT_ACCOUNT_ERROR:
                case WalletConstants.ERROR_CODE_SERVICE_UNAVAILABLE:
                case WalletConstants.ERROR_CODE_UNSUPPORTED_API_VERSION:
                case WalletConstants.ERROR_CODE_UNKNOWN:
                default:
                    // unrecoverable error
                   // mGoogleWalletDisabled = true;
                   // displayGoogleWalletErrorToast(errorCode);
                    break;
            }
        }

    }

这是我的日志:

12-05 06:01:37.437: E/AndroidRuntime(1660): FATAL EXCEPTION: main
12-05 06:01:37.437: E/AndroidRuntime(1660): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.main.udebate/com.main.udebate.Upgrade}: java.lang.NullPointerException: null reference
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.os.Looper.loop(Looper.java:137)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.main(ActivityThread.java:5041)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at java.lang.reflect.Method.invokeNative(Native Method)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at java.lang.reflect.Method.invoke(Method.java:511)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at dalvik.system.NativeStart.main(Native Method)
12-05 06:01:37.437: E/AndroidRuntime(1660): Caused by: java.lang.NullPointerException: null reference
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.internal.dm.e(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.internal.de.<init>(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.internal.gj.<init>(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.google.android.gms.wallet.WalletClient.<init>(Unknown Source)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at com.main.udebate.Upgrade.onCreate(Upgrade.java:56)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.Activity.performCreate(Activity.java:5104)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-05 06:01:37.437: E/AndroidRuntime(1660):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-05 06:01:37.437: E/AndroidRuntime(1660):     ... 11 more

【问题讨论】:

    标签: android android-pay


    【解决方案1】:

    我必须说我根本不熟悉钱包。但我认为将空值放入该函数是您的问题:

    ConnectionCallbacks connectionCallbackListener = null;
    OnConnectionFailedListener connectionFailedListener = null;
    mWalletClient = new WalletClient(this,
                                     WalletConstants.ENVIRONMENT_SANDBOX,
                                     "censored@example.com",
                                     WalletConstants.THEME_HOLO_LIGHT,
                                     connectionCallbackListener, // <-- this is null
                                     connectionFailedListener);  // <-- this is null
    

    我认为你只需要实现一个真正的监听器。

    【讨论】:

      【解决方案2】:

      您需要同时实现 GooglePlayServicesClient.ConnectionCallbacks 和 GooglePlayServicesClient.OnConnectionFailedListener。理想情况下,您可以让当前活动实现这两个接口并将“this”传递给 WalletClient 构造函数。

      请注意,SDK 管理器中包含一个示例应用程序,可在 /extras/google/google_play_services/samples/wallet 中找到。

      希望有帮助

      纳格什

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,但如果我执行以下操作,NPE 就消失了 让你的班级实现 OnConnectionFailedListener、ConnectionCallbacks 然后

        mWalletClient = new WalletClient(this, WalletConstants.ENVIRONMENT_SANDBOX, account_name, WalletConstants.THEME_HOLO_LIGHT, this, this);

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 2013-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-07
          • 2013-05-28
          • 1970-01-01
          • 2018-05-29
          • 1970-01-01
          相关资源
          最近更新 更多