查询库存中的空指针异常

时间:2015-02-17 16:48:29

标签: javascript android in-app-purchase

我正在尝试在我的应用程序中设置应用内结算。我在alpha测试中有它。我不断收到所有测试人员使用相同logcat的崩溃报告,如下所示:

java.lang.RuntimeException: Unable to start activity  
ComponentInfo{autonote.six.padc.autonote/autonote.six.padc.autonote.SaveScreen}:     
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.access$700(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5455)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at autonote.six.padc.autonote.SaveScreen.onCreate(SaveScreen.java:59)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
... 11 more

这似乎是一个简单的解决方案,只需转到第59行,对吧?错误。当我去那里时,抛出异常的行是:

 mHelper.queryInventoryAsync(mGotInventoryListener);

这就是我失去它的地方。 以下是我的代码的相关部分:

public class SaveScreen extends ActionBarActivity {
Button EMAIL_NOTES;
IabHelper mHelper;
String UPGRADE_CODE = "upgrade_autonote";
public static int VERSION_NUMBER = 534985739;
boolean mIsPremium;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_savescreen);
    //Query Inventory to see if user is premium
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener
            = new IabHelper.QueryInventoryFinishedListener() {
        public void onQueryInventoryFinished(IabResult result,
                                             Inventory inventory) {

            if (result.isFailure()) {
                // handle error here
            }
            else {
                // does the user have the premium upgrade?
                mIsPremium = inventory.hasPurchase(UPGRADE_CODE);
                VERSION_NUMBER = 434975736;
            }
        }
    };
    mHelper.queryInventoryAsync(mGotInventoryListener);
    //^^^^^^^^^^^^^^^null pointer^^^^^^^^^^^^^^^^^^^^^^^^


    EMAIL_NOTES.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(VERSION_NUMBER == 534985736){
                Toast.makeText(getBaseContext(), "You must upgrade to unlock this feature", Toast.LENGTH_LONG).show();

            }else if(VERSION_NUMBER == 434975739){
                Intent EMAIL_NOTES = new Intent(getBaseContext(), Email_Notes.class);
                EMAIL_NOTES.putExtra("serialized",cNotes_serialized);
                EMAIL_NOTES.putExtra("class_Name", cName);
                startActivity(EMAIL_NOTES);



            }
        }
    });

} 有任何想法如何解决这个问题?为了记录,我已经进去买了这个产品。它表示购买成功。谢谢大家!

1 个答案:

答案 0 :(得分:5)

这里:

  

mHelper.queryInventoryAsync(mGotInventoryListener);

NPE因为mHelpernull

通过使用当前Acitivty上下文和公钥调用mHelper构造函数来初始化IabHelper对象:

mHelper = new IabHelper(this, base64EncodedPublicKey);

有关详细信息What is base64EncodedPublicKey?,请参阅:

Preparing Your In-app Billing Application