使用应用内结算时出现NullPointerException?

时间:2013-01-18 19:19:43

标签: android nullpointerexception

我最近一直试图在我正在处理的游戏中实现应用计费,但是我得到了一个N​​ullPointerException,我只是看不出原因。

BuyActivity.java:

package com.liamw.games.whatami;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.liamw.games.whatami.util.IabHelper;
import com.liamw.games.whatami.util.IabResult;
import com.liamw.games.whatami.util.Inventory;
import com.liamw.games.whatami.util.Purchase;

public class BuyActivity extends Activity {

    String key, androidid;
    IabHelper mHelper;
    Button fifty;
    ProgressDialog wait;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // DONE Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy);

        fifty = (Button) findViewById(R.id.b50hints);
        androidid = Secure.getString(BuyActivity.this.getContentResolver(),
                Secure.ANDROID_ID);

        final ProgressDialog wait = new ProgressDialog(this);
        wait.setTitle("Updating...");
        wait.setMessage("Please wait while the details are loaded...");
        wait.setCancelable(false);
        wait.show();

        key = "xxxxx";
        mHelper = new IabHelper(this, key);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    Log.d("What Am I - IAB",
                            "Problem setting up In-app Billing: " + result);
                }
                // Hooray, IAB is fully set up!

                List<String> additionalSkuList = new ArrayList<String>();
                additionalSkuList.add("50hints");

                mHelper.queryInventoryAsync(true, additionalSkuList,
                        mQueryFinishedListener);

            }
        });

        fifty.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                mHelper.launchPurchaseFlow(BuyActivity.this, "50hints", 10001,
                        mPurchaseFinishedListener, androidid);
                fifty.setEnabled(false);

            }
        });

    }

    IabHelper.QueryInventoryFinishedListener mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                Inventory inventory) {
            if (result.isFailure()) {
                // handle error
                return;
            }

            String fiftyhintscost = inventory.getSkuDetails("50hints").getPrice();

            fifty.setText("50 Hints: " + fiftyhintscost);
            wait.dismiss();
            // update the UI
        }
    };

    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
        public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
            if (result.isFailure()) {
                Log.d("What Am I? IAB", "Error purchasing: " + result);
                return;
            } else if (purchase.getSku().equals("50hints")
                    && purchase.getDeveloperPayload().equals(androidid)) {
                // consume the gas and update the UI
                mHelper.consumeAsync(purchase, mConsumeFinishedListener);
            }
            fifty.setEnabled(true);
        }
    };

    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase, IabResult result) {
            if (result.isSuccess()) {
                // provision the in-app purchase to the user
                // (for example, credit 50 gold coins to player's character)
                Toast.makeText(BuyActivity.this,
                        "Purchase Successful - Crediting!", Toast.LENGTH_LONG)
                        .show();
            } else {
                // handle error
            }
        }
    };

    @Override
    public void onDestroy() {
        if (mHelper != null)
            mHelper.dispose();
        mHelper = null;
        super.onDestroy();
    }
}

logcat的:

01-18 19:12:45.829: E/AndroidRuntime(16849): FATAL EXCEPTION: main
01-18 19:12:45.829: E/AndroidRuntime(16849): java.lang.NullPointerException
01-18 19:12:45.829: E/AndroidRuntime(16849):    at com.liamw.games.whatami.BuyActivity$1.onQueryInventoryFinished(BuyActivity.java:87)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at com.liamw.games.whatami.util.IabHelper$2$1.run(IabHelper.java:533)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at android.os.Handler.handleCallback(Handler.java:725)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at android.os.Looper.loop(Looper.java:137)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at java.lang.reflect.Method.invokeNative(Native Method)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at java.lang.reflect.Method.invoke(Method.java:511)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-18 19:12:45.829: E/AndroidRuntime(16849):    at dalvik.system.NativeStart.main(Native Method)

第87行:

String fiftyhintscost = inventory.getSkuDetails("50hints").getPrice();

请帮我找到NullPointer的来源并告诉我如何修复它......

1 个答案:

答案 0 :(得分:1)

原来你只能使用SKU中的字母......