Note that there are some explanatory texts on larger screens.

plurals
  1. POIn App Purchase problems
    text
    copied!<p>Working on my latest app I am implementing In app purchases.</p> <p>I have looked for good tutorials online and found one from Bundell.</p> <p>I went over the code quite a few times, but I keep getting an Error. Looks like this:</p> <pre><code>01-30 21:14:17.415: E/AndroidRuntime(680): FATAL EXCEPTION: main 01-30 21:14:17.415: E/AndroidRuntime(680): java.lang.RuntimeException: Unable to destroy activity {com.crosscommunications.adalert/com.crosscommunications.adalert.inapppurchaseStarter}: java.lang.NullPointerException 01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2672) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2690) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.access$2100(ActivityThread.java:117) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:964) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.os.Handler.dispatchMessage(Handler.java:99) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.os.Looper.loop(Looper.java:130) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.main(ActivityThread.java:3683) 01-30 21:14:17.415: E/AndroidRuntime(680): at java.lang.reflect.Method.invokeNative(Native Method) 01-30 21:14:17.415: E/AndroidRuntime(680): at java.lang.reflect.Method.invoke(Method.java:507) 01-30 21:14:17.415: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 01-30 21:14:17.415: E/AndroidRuntime(680): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-30 21:14:17.415: E/AndroidRuntime(680): at dalvik.system.NativeStart.main(Native Method) 01-30 21:14:17.415: E/AndroidRuntime(680): Caused by: java.lang.NullPointerException 01-30 21:14:17.415: E/AndroidRuntime(680): at android.content.ComponentName.&lt;init&gt;(ComponentName.java:75) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.content.Intent.&lt;init&gt;(Intent.java:2702) 01-30 21:14:17.415: E/AndroidRuntime(680): at com.crosscommunications.adalert.BillingHelper.stopService(BillingHelper.java:261) 01-30 21:14:17.415: E/AndroidRuntime(680): at com.crosscommunications.adalert.inapppurchaseStarter.onDestroy(inapppurchaseStarter.java:129) 01-30 21:14:17.415: E/AndroidRuntime(680): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2659) 01-30 21:14:17.415: E/AndroidRuntime(680): ... 11 more </code></pre> <p>It's a nullpointerException but I can't figure out what is happening. I use this code to make an in app purchase:</p> <pre><code>package com.crosscommunications.adalert; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class inapppurchaseStarter extends Activity { private static final String TAG = "BillingService"; private Context mContext; private ImageView purchaseableItem; private Button purchaseButton; SessionManager session; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i("BillingService", "Starting"); setContentView(R.layout.purchasestarter); mContext = this; Button annuleren = (Button) findViewById(R.id.purchaseAnnuleren); Button starter = (Button) findViewById(R.id.koopStarter); purchaseableItem = (ImageView) findViewById(R.id.gekocht); startService(new Intent(mContext, BillingService.class)); BillingHelper.setCompletedHandler(mTransactionHandler); annuleren.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { finish(); } }); starter.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { if(BillingHelper.isBillingSupported()){ BillingHelper.requestPurchase(mContext, "android.test.purchased"); // android.test.purchased or android.test.canceled or android.test.refunded or com.blundell.item.passport } else { Log.i(TAG,"Can't purchase on this device"); purchaseButton.setEnabled(false); // XXX press button before service started will disable when it shouldnt } Log.i(TAG,"default. ID: "+v.getId()); } }); } public Handler mTransactionHandler = new Handler(){ public void handleMessage(android.os.Message msg) { Log.i(TAG, "Transaction complete"); Log.i(TAG, "Transaction status: "+BillingHelper.latestPurchase.purchaseState); Log.i(TAG, "Item purchased is: "+BillingHelper.latestPurchase.productId); if(BillingHelper.latestPurchase.isPurchased()){ showItem(); if(BillingHelper.latestPurchase.productId.equals("android.test.purchased")){ System.out.println("Starter is vanaf nu aangekocht"); } } }; }; public static Date addMonth(Date date, int i) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, i); return cal.getTime(); } private void showItem() { purchaseableItem.setVisibility(View.VISIBLE); Toast toast = Toast.makeText(this, "Product gekocht", Toast.LENGTH_LONG); toast.show(); } @Override protected void onPause() { Log.i(TAG, "onPause())"); super.onPause(); } @Override public void onDestroy() { super.onDestroy(); BillingHelper.stopService(); } } </code></pre> <p>You see that i've got a button, and when clicked on it's gonna finish the activity. every time I finish the activity i get this error....</p> <p>When onDestroy gets called then things go bad.. I think it has something to do with the stopService() method.</p> <p>that method looks like this:</p> <pre><code>public static void stopService(){ mContext.stopService(new Intent(mContext, BillingService.class)); mService = null; mContext = null; mCompletedHandler = null; Log.i(TAG, "Stopping Service"); } </code></pre> <p>Please guys.. Can you give me tips/pointers or just an explanation?</p> <p>EDIT</p> <p>Ok... Done that... I've removed onDestroy and copied the stopService to before the finish method... Getting another nullpointerexception.. Kinda the same:</p> <pre><code>01-30 21:51:07.135: E/AndroidRuntime(741): FATAL EXCEPTION: main 01-30 21:51:07.135: E/AndroidRuntime(741): java.lang.NullPointerException 01-30 21:51:07.135: E/AndroidRuntime(741): at android.content.ComponentName.&lt;init&gt;(ComponentName.java:75) 01-30 21:51:07.135: E/AndroidRuntime(741): at android.content.Intent.&lt;init&gt;(Intent.java:2702) 01-30 21:51:07.135: E/AndroidRuntime(741): at com.crosscommunications.adalert.BillingHelper.stopService(BillingHelper.java:261) 01-30 21:51:07.135: E/AndroidRuntime(741): at com.crosscommunications.adalert.inapppurchaseStarter$2.onClick(inapppurchaseStarter.java:48) 01-30 21:51:07.135: E/AndroidRuntime(741): at android.view.View.performClick(View.java:2485) 01-30 21:51:07.135: E/AndroidRuntime(741): at android.view.View$PerformClick.run(View.java:9080) 01-30 21:51:07.135: E/AndroidRuntime(741): at android.os.Handler.handleCallback(Handler.java:587) 01-30 21:51:07.135: E/AndroidRuntime(741): at android.os.Handler.dispatchMessage(Handler.java:92) 01-30 21:51:07.135: E/AndroidRuntime(741): at android.os.Looper.loop(Looper.java:130) 01-30 21:51:07.135: E/AndroidRuntime(741): at android.app.ActivityThread.main(ActivityThread.java:3683) 01-30 21:51:07.135: E/AndroidRuntime(741): at java.lang.reflect.Method.invokeNative(Native Method) 01-30 21:51:07.135: E/AndroidRuntime(741): at java.lang.reflect.Method.invoke(Method.java:507) 01-30 21:51:07.135: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 01-30 21:51:07.135: E/AndroidRuntime(741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-30 21:51:07.135: E/AndroidRuntime(741): at dalvik.system.NativeStart.main(Native Method) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload