Question

I try to get skus like they say in Android docs:

IInAppBillingService mService;
ServiceConnection mServiceConn = new ServiceConnection() {
    @Override
    public void onServiceDisconnected(ComponentName name) {
        mService = null;
    }
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = IInAppBillingService.Stub.asInterface(service);          
    }
};

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    ArrayList<String> skuList = new ArrayList<String>();
    skuList.add("no_ads");
    Bundle querySkus = new Bundle();
    querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
    Bundle skuDetails = new Bundle();
    try {
        skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}   

LogCat:

07-28 19:48:33.243: E/AndroidRuntime(24103): FATAL EXCEPTION: main
07-28 19:48:33.243: E/AndroidRuntime(24103): Process: com.example, PID: 24103
07-28 19:48:33.243: E/AndroidRuntime(24103): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.AndroidLauncher}: java.lang.NullPointerException
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.os.Looper.loop(Looper.java:136)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.ActivityThread.main(ActivityThread.java:5017)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at java.lang.reflect.Method.invokeNative(Native Method)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at java.lang.reflect.Method.invoke(Method.java:515)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at dalvik.system.NativeStart.main(Native Method)
07-28 19:48:33.243: E/AndroidRuntime(24103): Caused by: java.lang.NullPointerException
07-28 19:48:33.243: E/AndroidRuntime(24103):    at com.example.AndroidLauncher.onCreate(AndroidLauncher.java:138)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.Activity.performCreate(Activity.java:5231)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-28 19:48:33.243: E/AndroidRuntime(24103):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

So, the problem is that the line skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus); causes a Null Pointer Exception (Eclipse forced me to put it inside try/catch, but it's not caught anyway) and can't figure out why. This should be trivial to implement, but somehow I fail at the most basic setup.

EDIT: OK, I got it - mService was null because it was not connected yet. How do I wait until onServiceConnected is called before I try to do anything with in-app billing? Surely there is some standard way to do this - obviously something that goes without saying because Google didn't mention it in their docs.

Was it helpful?

Solution

you try to execute

        skuDetails = mService.getSkuDetails(3, getPackageName(), "inapp", querySkus);

in onCreate - but mService might not be there yet ( is async ) - you might want to do this after the onServiceConnected call

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top