getPurchases()上的NullPointerException

时间:2013-07-23 21:22:40

标签: android android-asynctask nullpointerexception in-app-billing serviceconnection

我正在尝试购买用户,我声称我的结算服务为空,该过程在AsyncTask中运行,顺便说一下我是Inapp计费服务的新手,这里是我的代码的一部分:

我的服务连接:

IInAppBillingService BillingService;

private ServiceConnection ServiceConnection = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {

                BillingService = null ;
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                BillingService = IInAppBillingService.Stub.asInterface(service);
            }
        };

我的AsyncTask的一部分:

@Override
        protected String doInBackground(Activity... params) {
            final Activity mActivity = params[0];

            bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), ServiceConnection, Context.BIND_AUTO_CREATE);


ConnectivityManager c = (ConnectivityManager) getSystemService(Service.CONNECTIVITY_SERVICE);
            NetworkInfo NI = c.getActiveNetworkInfo();

            if(NI == null || ! NI.isConnected()){

                    // do some stuff

            }else{
                Bundle purchases = null;

                try {                


                    purchases = 
                            BillingService.
                            getPurchases(3, "le.wild.packagename", "inapp", null);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                if(purchases.getInt("RESPONSE_CODE") == 0){
                    ArrayList<String> Items = purchases.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
                    ArrayList<String> itemsData = purchases.getStringArrayList("INAPP_PURCHASE_DATA_LIST");

                    boolean o = false;

                    for(int i = 0 ; itemsData.size() > i ; i++){
                        if(Items.get(i).contains("le.wild.itemName")){
                            o = false;
                            continue;
                        }else{
                            o = true;
                        }
                    }
                    // do some stuff
            }

我可以使用相同的方式消费购买和购买意图和其他功能,只有在这里,现在它不工作!如果您需要更多信息或任何事情,请提前感谢!

编辑:

StackTrace:

07-22 15:58:12.830: E/AndroidRuntime(12719): FATAL EXCEPTION: AsyncTask #1
07-22 15:58:12.830: E/AndroidRuntime(12719): java.lang.RuntimeException: An error occured while executing doInBackground()
07-22 15:58:12.830: E/AndroidRuntime(12719):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at java.lang.Thread.run(Thread.java:856)
07-22 15:58:12.830: E/AndroidRuntime(12719): Caused by: java.lang.NullPointerException
07-22 15:58:12.830: E/AndroidRuntime(12719):    at seaskyways.quadcoremanagerplus.CoresManagement$2.doInBackground(CoresManagement.java:372)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at seaskyways.quadcoremanagerplus.CoresManagement$2.doInBackground(CoresManagement.java:1)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-22 15:58:12.830: E/AndroidRuntime(12719):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-22 15:58:12.830: E/AndroidRuntime(12719):    ... 4 more

2 个答案:

答案 0 :(得分:1)

bindService是异步的。因此,在调用onServiceConnected之前需要一点时间来设置接口。所以你应该在实际需要调用它之前更早地绑定服务。

同样以大写字母命名变量令人困惑。

答案 1 :(得分:0)

只需在getPurchases内拨打onServiceConnected,您就安全了。