Reputation: 3795
I am trying to get the purchases of the user , and I claim my Billing Service is null , the process is running in an AsyncTask ,btw I am a newbie to Inapp billing service , here are parts of my code :
my Service Connection :
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);
}
};
Part of my 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
}
I can am using the same way for consuming purchases and getting buy intents and other functions , only here and now its not working ! If you need anymore info or anything am here , thanks in advance !
EDIT :
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
Upvotes: 0
Views: 888
Reputation: 1002
Just call getPurchases
only inside onServiceConnected
and you are safe.
Upvotes: 0
Reputation: 909
bindService is async. So it will take a little bit of time before your onServiceConnected is called back set the interface. So you should bind with service much much earlier before you actually need to call it.
Also naming your variable starting with uppercase is confusing.
Upvotes: 1