在App购买问题

时间:2013-01-30 21:27:38

标签: android android-service android-handler android-billing

使用我的最新应用,我正在实施应用内购买。

我在线寻找好的教程,并在Bundell找到了一个。

我多次查看代码,但是我一直收到错误。看起来像这样:

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.<init>(ComponentName.java:75)
01-30 21:14:17.415: E/AndroidRuntime(680):  at android.content.Intent.<init>(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

这是一个nullpointerException,但我无法弄清楚发生了什么。我使用此代码进行应用内购买:

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();
        }

    }

你看到我有一个按钮,当点击它时,它将完成活动。每次我完成活动,我都会收到这个错误......

当onDestroy被调用时,事情就变坏了......我认为它与stopService()方法有关。

该方法如下所示:

public static void stopService(){
    mContext.stopService(new Intent(mContext, BillingService.class));
    mService = null;
    mContext = null;
    mCompletedHandler = null;
    Log.i(TAG, "Stopping Service");
}

请大家..你能给我一些提示/指示或只是一个解释吗?

修改

好的......完成了......我删除了onDestroy并将stopService复制到了finish方法之前...获得另一个nullpointerexception ..有点相同:

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.<init>(ComponentName.java:75)
01-30 21:51:07.135: E/AndroidRuntime(741):  at android.content.Intent.<init>(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)

1 个答案:

答案 0 :(得分:0)

mContext是您活动的一部分。由于您已经在活动上调用了finish(),因此可能会对mContext进行垃圾回收。 在致电stopService之前,您可能需要致电finish